]> git.proxmox.com Git - rustc.git/blob - src/librustc_codegen_ssa/back/linker.rs
New upstream version 1.38.0+dfsg1
[rustc.git] / src / librustc_codegen_ssa / back / linker.rs
1 use super::symbol_export;
2 use super::command::Command;
3 use super::archive;
4
5 use rustc_data_structures::fx::FxHashMap;
6 use std::ffi::{OsStr, OsString};
7 use std::fs::{self, File};
8 use std::io::prelude::*;
9 use std::io::{self, BufWriter};
10 use std::path::{Path, PathBuf};
11
12 use rustc::hir::def_id::{LOCAL_CRATE, CrateNum};
13 use rustc::middle::dependency_format::Linkage;
14 use rustc::session::Session;
15 use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
16 LinkerPluginLto, Lto};
17 use rustc::ty::TyCtxt;
18 use rustc_target::spec::{LinkerFlavor, LldFlavor};
19 use rustc_serialize::{json, Encoder};
20
21 /// For all the linkers we support, and information they might
22 /// need out of the shared crate context before we get rid of it.
23 pub struct LinkerInfo {
24 exports: FxHashMap<CrateType, Vec<String>>,
25 }
26
27 impl LinkerInfo {
28 pub fn new(tcx: TyCtxt<'_>) -> LinkerInfo {
29 LinkerInfo {
30 exports: tcx.sess.crate_types.borrow().iter().map(|&c| {
31 (c, exported_symbols(tcx, c))
32 }).collect(),
33 }
34 }
35
36 pub fn to_linker<'a>(
37 &'a self,
38 cmd: Command,
39 sess: &'a Session,
40 flavor: LinkerFlavor,
41 target_cpu: &'a str,
42 ) -> Box<dyn Linker+'a> {
43 match flavor {
44 LinkerFlavor::Lld(LldFlavor::Link) |
45 LinkerFlavor::Msvc => {
46 Box::new(MsvcLinker {
47 cmd,
48 sess,
49 info: self
50 }) as Box<dyn Linker>
51 }
52 LinkerFlavor::Em => {
53 Box::new(EmLinker {
54 cmd,
55 sess,
56 info: self
57 }) as Box<dyn Linker>
58 }
59 LinkerFlavor::Gcc => {
60 Box::new(GccLinker {
61 cmd,
62 sess,
63 info: self,
64 hinted_static: false,
65 is_ld: false,
66 target_cpu,
67 }) as Box<dyn Linker>
68 }
69
70 LinkerFlavor::Lld(LldFlavor::Ld) |
71 LinkerFlavor::Lld(LldFlavor::Ld64) |
72 LinkerFlavor::Ld => {
73 Box::new(GccLinker {
74 cmd,
75 sess,
76 info: self,
77 hinted_static: false,
78 is_ld: true,
79 target_cpu,
80 }) as Box<dyn Linker>
81 }
82
83 LinkerFlavor::Lld(LldFlavor::Wasm) => {
84 Box::new(WasmLd::new(cmd, sess, self)) as Box<dyn Linker>
85 }
86
87 LinkerFlavor::PtxLinker => {
88 Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>
89 }
90 }
91 }
92 }
93
94 /// Linker abstraction used by `back::link` to build up the command to invoke a
95 /// linker.
96 ///
97 /// This trait is the total list of requirements needed by `back::link` and
98 /// represents the meaning of each option being passed down. This trait is then
99 /// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
100 /// MSVC linker (e.g., `link.exe`) is being used.
101 pub trait Linker {
102 fn link_dylib(&mut self, lib: &str);
103 fn link_rust_dylib(&mut self, lib: &str, path: &Path);
104 fn link_framework(&mut self, framework: &str);
105 fn link_staticlib(&mut self, lib: &str);
106 fn link_rlib(&mut self, lib: &Path);
107 fn link_whole_rlib(&mut self, lib: &Path);
108 fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]);
109 fn include_path(&mut self, path: &Path);
110 fn framework_path(&mut self, path: &Path);
111 fn output_filename(&mut self, path: &Path);
112 fn add_object(&mut self, path: &Path);
113 fn gc_sections(&mut self, keep_metadata: bool);
114 fn position_independent_executable(&mut self);
115 fn no_position_independent_executable(&mut self);
116 fn full_relro(&mut self);
117 fn partial_relro(&mut self);
118 fn no_relro(&mut self);
119 fn optimize(&mut self);
120 fn pgo_gen(&mut self);
121 fn debuginfo(&mut self);
122 fn no_default_libraries(&mut self);
123 fn build_dylib(&mut self, out_filename: &Path);
124 fn build_static_executable(&mut self);
125 fn args(&mut self, args: &[String]);
126 fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
127 fn subsystem(&mut self, subsystem: &str);
128 fn group_start(&mut self);
129 fn group_end(&mut self);
130 fn linker_plugin_lto(&mut self);
131 // Should have been finalize(self), but we don't support self-by-value on trait objects (yet?).
132 fn finalize(&mut self) -> Command;
133 }
134
135 pub struct GccLinker<'a> {
136 cmd: Command,
137 sess: &'a Session,
138 info: &'a LinkerInfo,
139 hinted_static: bool, // Keeps track of the current hinting mode.
140 // Link as ld
141 is_ld: bool,
142 target_cpu: &'a str,
143 }
144
145 impl<'a> GccLinker<'a> {
146 /// Argument that must be passed *directly* to the linker
147 ///
148 /// These arguments need to be prepended with `-Wl`, when a GCC-style linker is used.
149 fn linker_arg<S>(&mut self, arg: S) -> &mut Self
150 where S: AsRef<OsStr>
151 {
152 if !self.is_ld {
153 let mut os = OsString::from("-Wl,");
154 os.push(arg.as_ref());
155 self.cmd.arg(os);
156 } else {
157 self.cmd.arg(arg);
158 }
159 self
160 }
161
162 fn takes_hints(&self) -> bool {
163 // Really this function only returns true if the underlying linker
164 // configured for a compiler is binutils `ld.bfd` and `ld.gold`. We
165 // don't really have a foolproof way to detect that, so rule out some
166 // platforms where currently this is guaranteed to *not* be the case:
167 //
168 // * On OSX they have their own linker, not binutils'
169 // * For WebAssembly the only functional linker is LLD, which doesn't
170 // support hint flags
171 !self.sess.target.target.options.is_like_osx &&
172 self.sess.target.target.arch != "wasm32"
173 }
174
175 // Some platforms take hints about whether a library is static or dynamic.
176 // For those that support this, we ensure we pass the option if the library
177 // was flagged "static" (most defaults are dynamic) to ensure that if
178 // libfoo.a and libfoo.so both exist that the right one is chosen.
179 fn hint_static(&mut self) {
180 if !self.takes_hints() { return }
181 if !self.hinted_static {
182 self.linker_arg("-Bstatic");
183 self.hinted_static = true;
184 }
185 }
186
187 fn hint_dynamic(&mut self) {
188 if !self.takes_hints() { return }
189 if self.hinted_static {
190 self.linker_arg("-Bdynamic");
191 self.hinted_static = false;
192 }
193 }
194
195 fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
196 if let Some(plugin_path) = plugin_path {
197 let mut arg = OsString::from("-plugin=");
198 arg.push(plugin_path);
199 self.linker_arg(&arg);
200 }
201
202 let opt_level = match self.sess.opts.optimize {
203 config::OptLevel::No => "O0",
204 config::OptLevel::Less => "O1",
205 config::OptLevel::Default => "O2",
206 config::OptLevel::Aggressive => "O3",
207 config::OptLevel::Size => "Os",
208 config::OptLevel::SizeMin => "Oz",
209 };
210
211 self.linker_arg(&format!("-plugin-opt={}", opt_level));
212 let target_cpu = self.target_cpu;
213 self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
214 }
215 }
216
217 impl<'a> Linker for GccLinker<'a> {
218 fn link_dylib(&mut self, lib: &str) { self.hint_dynamic(); self.cmd.arg(format!("-l{}", lib)); }
219 fn link_staticlib(&mut self, lib: &str) {
220 self.hint_static(); self.cmd.arg(format!("-l{}", lib));
221 }
222 fn link_rlib(&mut self, lib: &Path) { self.hint_static(); self.cmd.arg(lib); }
223 fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); }
224 fn framework_path(&mut self, path: &Path) { self.cmd.arg("-F").arg(path); }
225 fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
226 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
227 fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
228 fn no_position_independent_executable(&mut self) { self.cmd.arg("-no-pie"); }
229 fn full_relro(&mut self) { self.linker_arg("-zrelro"); self.linker_arg("-znow"); }
230 fn partial_relro(&mut self) { self.linker_arg("-zrelro"); }
231 fn no_relro(&mut self) { self.linker_arg("-znorelro"); }
232 fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
233 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
234
235 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
236 self.hint_dynamic();
237 self.cmd.arg(format!("-l{}", lib));
238 }
239
240 fn link_framework(&mut self, framework: &str) {
241 self.hint_dynamic();
242 self.cmd.arg("-framework").arg(framework);
243 }
244
245 // Here we explicitly ask that the entire archive is included into the
246 // result artifact. For more details see #15460, but the gist is that
247 // the linker will strip away any unused objects in the archive if we
248 // don't otherwise explicitly reference them. This can occur for
249 // libraries which are just providing bindings, libraries with generic
250 // functions, etc.
251 fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
252 self.hint_static();
253 let target = &self.sess.target.target;
254 if !target.options.is_like_osx {
255 self.linker_arg("--whole-archive").cmd.arg(format!("-l{}", lib));
256 self.linker_arg("--no-whole-archive");
257 } else {
258 // -force_load is the macOS equivalent of --whole-archive, but it
259 // involves passing the full path to the library to link.
260 self.linker_arg("-force_load");
261 let lib = archive::find_library(lib, search_path, &self.sess);
262 self.linker_arg(&lib);
263 }
264 }
265
266 fn link_whole_rlib(&mut self, lib: &Path) {
267 self.hint_static();
268 if self.sess.target.target.options.is_like_osx {
269 self.linker_arg("-force_load");
270 self.linker_arg(&lib);
271 } else {
272 self.linker_arg("--whole-archive").cmd.arg(lib);
273 self.linker_arg("--no-whole-archive");
274 }
275 }
276
277 fn gc_sections(&mut self, keep_metadata: bool) {
278 // The dead_strip option to the linker specifies that functions and data
279 // unreachable by the entry point will be removed. This is quite useful
280 // with Rust's compilation model of compiling libraries at a time into
281 // one object file. For example, this brings hello world from 1.7MB to
282 // 458K.
283 //
284 // Note that this is done for both executables and dynamic libraries. We
285 // won't get much benefit from dylibs because LLVM will have already
286 // stripped away as much as it could. This has not been seen to impact
287 // link times negatively.
288 //
289 // -dead_strip can't be part of the pre_link_args because it's also used
290 // for partial linking when using multiple codegen units (-r). So we
291 // insert it here.
292 if self.sess.target.target.options.is_like_osx {
293 self.linker_arg("-dead_strip");
294 } else if self.sess.target.target.options.is_like_solaris {
295 self.linker_arg("-zignore");
296
297 // If we're building a dylib, we don't use --gc-sections because LLVM
298 // has already done the best it can do, and we also don't want to
299 // eliminate the metadata. If we're building an executable, however,
300 // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
301 // reduction.
302 } else if !keep_metadata {
303 self.linker_arg("--gc-sections");
304 }
305 }
306
307 fn optimize(&mut self) {
308 if !self.sess.target.target.options.linker_is_gnu { return }
309
310 // GNU-style linkers support optimization with -O. GNU ld doesn't
311 // need a numeric argument, but other linkers do.
312 if self.sess.opts.optimize == config::OptLevel::Default ||
313 self.sess.opts.optimize == config::OptLevel::Aggressive {
314 self.linker_arg("-O1");
315 }
316 }
317
318 fn pgo_gen(&mut self) {
319 if !self.sess.target.target.options.linker_is_gnu { return }
320
321 // If we're doing PGO generation stuff and on a GNU-like linker, use the
322 // "-u" flag to properly pull in the profiler runtime bits.
323 //
324 // This is because LLVM otherwise won't add the needed initialization
325 // for us on Linux (though the extra flag should be harmless if it
326 // does).
327 //
328 // See https://reviews.llvm.org/D14033 and https://reviews.llvm.org/D14030.
329 //
330 // Though it may be worth to try to revert those changes upstream, since
331 // the overhead of the initialization should be minor.
332 self.cmd.arg("-u");
333 self.cmd.arg("__llvm_profile_runtime");
334 }
335
336 fn debuginfo(&mut self) {
337 if let DebugInfo::None = self.sess.opts.debuginfo {
338 // If we are building without debuginfo enabled and we were called with
339 // `-Zstrip-debuginfo-if-disabled=yes`, tell the linker to strip any debuginfo
340 // found when linking to get rid of symbols from libstd.
341 if let Some(true) = self.sess.opts.debugging_opts.strip_debuginfo_if_disabled {
342 self.linker_arg("-S");
343 }
344 };
345 }
346
347 fn no_default_libraries(&mut self) {
348 if !self.is_ld {
349 self.cmd.arg("-nodefaultlibs");
350 }
351 }
352
353 fn build_dylib(&mut self, out_filename: &Path) {
354 // On mac we need to tell the linker to let this library be rpathed
355 if self.sess.target.target.options.is_like_osx {
356 self.cmd.arg("-dynamiclib");
357 self.linker_arg("-dylib");
358
359 // Note that the `osx_rpath_install_name` option here is a hack
360 // purely to support rustbuild right now, we should get a more
361 // principled solution at some point to force the compiler to pass
362 // the right `-Wl,-install_name` with an `@rpath` in it.
363 if self.sess.opts.cg.rpath || self.sess.opts.debugging_opts.osx_rpath_install_name {
364 self.linker_arg("-install_name");
365 let mut v = OsString::from("@rpath/");
366 v.push(out_filename.file_name().unwrap());
367 self.linker_arg(&v);
368 }
369 } else {
370 self.cmd.arg("-shared");
371 if self.sess.target.target.options.is_like_windows {
372 // The output filename already contains `dll_suffix` so
373 // the resulting import library will have a name in the
374 // form of libfoo.dll.a
375 let implib_name = out_filename
376 .file_name()
377 .and_then(|file| file.to_str())
378 .map(|file| format!("{}{}{}",
379 self.sess.target.target.options.staticlib_prefix,
380 file,
381 self.sess.target.target.options.staticlib_suffix));
382 if let Some(implib_name) = implib_name {
383 let implib = out_filename
384 .parent()
385 .map(|dir| dir.join(&implib_name));
386 if let Some(implib) = implib {
387 self.linker_arg(&format!("--out-implib,{}", (*implib).to_str().unwrap()));
388 }
389 }
390 }
391 }
392 }
393
394 fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
395 // Symbol visibility in object files typically takes care of this.
396 if crate_type == CrateType::Executable {
397 return;
398 }
399
400 // We manually create a list of exported symbols to ensure we don't expose any more.
401 // The object files have far more public symbols than we actually want to export,
402 // so we hide them all here.
403
404 if !self.sess.target.target.options.limit_rdylib_exports {
405 return;
406 }
407
408 if crate_type == CrateType::ProcMacro {
409 return
410 }
411
412 let mut arg = OsString::new();
413 let path = tmpdir.join("list");
414
415 debug!("EXPORTED SYMBOLS:");
416
417 if self.sess.target.target.options.is_like_osx {
418 // Write a plain, newline-separated list of symbols
419 let res: io::Result<()> = try {
420 let mut f = BufWriter::new(File::create(&path)?);
421 for sym in self.info.exports[&crate_type].iter() {
422 debug!(" _{}", sym);
423 writeln!(f, "_{}", sym)?;
424 }
425 };
426 if let Err(e) = res {
427 self.sess.fatal(&format!("failed to write lib.def file: {}", e));
428 }
429 } else {
430 // Write an LD version script
431 let res: io::Result<()> = try {
432 let mut f = BufWriter::new(File::create(&path)?);
433 writeln!(f, "{{\n global:")?;
434 for sym in self.info.exports[&crate_type].iter() {
435 debug!(" {};", sym);
436 writeln!(f, " {};", sym)?;
437 }
438 writeln!(f, "\n local:\n *;\n}};")?;
439 };
440 if let Err(e) = res {
441 self.sess.fatal(&format!("failed to write version script: {}", e));
442 }
443 }
444
445 if self.sess.target.target.options.is_like_osx {
446 if !self.is_ld {
447 arg.push("-Wl,")
448 }
449 arg.push("-exported_symbols_list,");
450 } else if self.sess.target.target.options.is_like_solaris {
451 if !self.is_ld {
452 arg.push("-Wl,")
453 }
454 arg.push("-M,");
455 } else {
456 if !self.is_ld {
457 arg.push("-Wl,")
458 }
459 arg.push("--version-script=");
460 }
461
462 arg.push(&path);
463 self.cmd.arg(arg);
464 }
465
466 fn subsystem(&mut self, subsystem: &str) {
467 self.linker_arg("--subsystem");
468 self.linker_arg(&subsystem);
469 }
470
471 fn finalize(&mut self) -> Command {
472 self.hint_dynamic(); // Reset to default before returning the composed command line.
473
474 ::std::mem::replace(&mut self.cmd, Command::new(""))
475 }
476
477 fn group_start(&mut self) {
478 if self.takes_hints() {
479 self.linker_arg("--start-group");
480 }
481 }
482
483 fn group_end(&mut self) {
484 if self.takes_hints() {
485 self.linker_arg("--end-group");
486 }
487 }
488
489 fn linker_plugin_lto(&mut self) {
490 match self.sess.opts.cg.linker_plugin_lto {
491 LinkerPluginLto::Disabled => {
492 // Nothing to do
493 }
494 LinkerPluginLto::LinkerPluginAuto => {
495 self.push_linker_plugin_lto_args(None);
496 }
497 LinkerPluginLto::LinkerPlugin(ref path) => {
498 self.push_linker_plugin_lto_args(Some(path.as_os_str()));
499 }
500 }
501 }
502 }
503
504 pub struct MsvcLinker<'a> {
505 cmd: Command,
506 sess: &'a Session,
507 info: &'a LinkerInfo
508 }
509
510 impl<'a> Linker for MsvcLinker<'a> {
511 fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
512 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
513 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
514
515 fn build_dylib(&mut self, out_filename: &Path) {
516 self.cmd.arg("/DLL");
517 let mut arg: OsString = "/IMPLIB:".into();
518 arg.push(out_filename.with_extension("dll.lib"));
519 self.cmd.arg(arg);
520 }
521
522 fn build_static_executable(&mut self) {
523 // noop
524 }
525
526 fn gc_sections(&mut self, _keep_metadata: bool) {
527 // MSVC's ICF (Identical COMDAT Folding) link optimization is
528 // slow for Rust and thus we disable it by default when not in
529 // optimization build.
530 if self.sess.opts.optimize != config::OptLevel::No {
531 self.cmd.arg("/OPT:REF,ICF");
532 } else {
533 // It is necessary to specify NOICF here, because /OPT:REF
534 // implies ICF by default.
535 self.cmd.arg("/OPT:REF,NOICF");
536 }
537 }
538
539 fn link_dylib(&mut self, lib: &str) {
540 self.cmd.arg(&format!("{}.lib", lib));
541 }
542
543 fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
544 // When producing a dll, the MSVC linker may not actually emit a
545 // `foo.lib` file if the dll doesn't actually export any symbols, so we
546 // check to see if the file is there and just omit linking to it if it's
547 // not present.
548 let name = format!("{}.dll.lib", lib);
549 if fs::metadata(&path.join(&name)).is_ok() {
550 self.cmd.arg(name);
551 }
552 }
553
554 fn link_staticlib(&mut self, lib: &str) {
555 self.cmd.arg(&format!("{}.lib", lib));
556 }
557
558 fn position_independent_executable(&mut self) {
559 // noop
560 }
561
562 fn no_position_independent_executable(&mut self) {
563 // noop
564 }
565
566 fn full_relro(&mut self) {
567 // noop
568 }
569
570 fn partial_relro(&mut self) {
571 // noop
572 }
573
574 fn no_relro(&mut self) {
575 // noop
576 }
577
578 fn no_default_libraries(&mut self) {
579 // Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
580 // as there's been trouble in the past of linking the C++ standard
581 // library required by LLVM. This likely needs to happen one day, but
582 // in general Windows is also a more controlled environment than
583 // Unix, so it's not necessarily as critical that this be implemented.
584 //
585 // Note that there are also some licensing worries about statically
586 // linking some libraries which require a specific agreement, so it may
587 // not ever be possible for us to pass this flag.
588 }
589
590 fn include_path(&mut self, path: &Path) {
591 let mut arg = OsString::from("/LIBPATH:");
592 arg.push(path);
593 self.cmd.arg(&arg);
594 }
595
596 fn output_filename(&mut self, path: &Path) {
597 let mut arg = OsString::from("/OUT:");
598 arg.push(path);
599 self.cmd.arg(&arg);
600 }
601
602 fn framework_path(&mut self, _path: &Path) {
603 bug!("frameworks are not supported on windows")
604 }
605 fn link_framework(&mut self, _framework: &str) {
606 bug!("frameworks are not supported on windows")
607 }
608
609 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
610 // not supported?
611 self.link_staticlib(lib);
612 }
613 fn link_whole_rlib(&mut self, path: &Path) {
614 // not supported?
615 self.link_rlib(path);
616 }
617 fn optimize(&mut self) {
618 // Needs more investigation of `/OPT` arguments
619 }
620
621 fn pgo_gen(&mut self) {
622 // Nothing needed here.
623 }
624
625 fn debuginfo(&mut self) {
626 // This will cause the Microsoft linker to generate a PDB file
627 // from the CodeView line tables in the object files.
628 self.cmd.arg("/DEBUG");
629
630 // This will cause the Microsoft linker to embed .natvis info into the PDB file
631 let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc");
632 if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
633 for entry in natvis_dir {
634 match entry {
635 Ok(entry) => {
636 let path = entry.path();
637 if path.extension() == Some("natvis".as_ref()) {
638 let mut arg = OsString::from("/NATVIS:");
639 arg.push(path);
640 self.cmd.arg(arg);
641 }
642 },
643 Err(err) => {
644 self.sess.warn(&format!("error enumerating natvis directory: {}", err));
645 },
646 }
647 }
648 }
649 }
650
651 // Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
652 // export symbols from a dynamic library. When building a dynamic library,
653 // however, we're going to want some symbols exported, so this function
654 // generates a DEF file which lists all the symbols.
655 //
656 // The linker will read this `*.def` file and export all the symbols from
657 // the dynamic library. Note that this is not as simple as just exporting
658 // all the symbols in the current crate (as specified by `codegen.reachable`)
659 // but rather we also need to possibly export the symbols of upstream
660 // crates. Upstream rlibs may be linked statically to this dynamic library,
661 // in which case they may continue to transitively be used and hence need
662 // their symbols exported.
663 fn export_symbols(&mut self,
664 tmpdir: &Path,
665 crate_type: CrateType) {
666 // Symbol visibility takes care of this typically
667 if crate_type == CrateType::Executable {
668 return;
669 }
670
671 let path = tmpdir.join("lib.def");
672 let res: io::Result<()> = try {
673 let mut f = BufWriter::new(File::create(&path)?);
674
675 // Start off with the standard module name header and then go
676 // straight to exports.
677 writeln!(f, "LIBRARY")?;
678 writeln!(f, "EXPORTS")?;
679 for symbol in self.info.exports[&crate_type].iter() {
680 debug!(" _{}", symbol);
681 writeln!(f, " {}", symbol)?;
682 }
683 };
684 if let Err(e) = res {
685 self.sess.fatal(&format!("failed to write lib.def file: {}", e));
686 }
687 let mut arg = OsString::from("/DEF:");
688 arg.push(path);
689 self.cmd.arg(&arg);
690 }
691
692 fn subsystem(&mut self, subsystem: &str) {
693 // Note that previous passes of the compiler validated this subsystem,
694 // so we just blindly pass it to the linker.
695 self.cmd.arg(&format!("/SUBSYSTEM:{}", subsystem));
696
697 // Windows has two subsystems we're interested in right now, the console
698 // and windows subsystems. These both implicitly have different entry
699 // points (starting symbols). The console entry point starts with
700 // `mainCRTStartup` and the windows entry point starts with
701 // `WinMainCRTStartup`. These entry points, defined in system libraries,
702 // will then later probe for either `main` or `WinMain`, respectively to
703 // start the application.
704 //
705 // In Rust we just always generate a `main` function so we want control
706 // to always start there, so we force the entry point on the windows
707 // subsystem to be `mainCRTStartup` to get everything booted up
708 // correctly.
709 //
710 // For more information see RFC #1665
711 if subsystem == "windows" {
712 self.cmd.arg("/ENTRY:mainCRTStartup");
713 }
714 }
715
716 fn finalize(&mut self) -> Command {
717 ::std::mem::replace(&mut self.cmd, Command::new(""))
718 }
719
720 // MSVC doesn't need group indicators
721 fn group_start(&mut self) {}
722 fn group_end(&mut self) {}
723
724 fn linker_plugin_lto(&mut self) {
725 // Do nothing
726 }
727 }
728
729 pub struct EmLinker<'a> {
730 cmd: Command,
731 sess: &'a Session,
732 info: &'a LinkerInfo
733 }
734
735 impl<'a> Linker for EmLinker<'a> {
736 fn include_path(&mut self, path: &Path) {
737 self.cmd.arg("-L").arg(path);
738 }
739
740 fn link_staticlib(&mut self, lib: &str) {
741 self.cmd.arg("-l").arg(lib);
742 }
743
744 fn output_filename(&mut self, path: &Path) {
745 self.cmd.arg("-o").arg(path);
746 }
747
748 fn add_object(&mut self, path: &Path) {
749 self.cmd.arg(path);
750 }
751
752 fn link_dylib(&mut self, lib: &str) {
753 // Emscripten always links statically
754 self.link_staticlib(lib);
755 }
756
757 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
758 // not supported?
759 self.link_staticlib(lib);
760 }
761
762 fn link_whole_rlib(&mut self, lib: &Path) {
763 // not supported?
764 self.link_rlib(lib);
765 }
766
767 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
768 self.link_dylib(lib);
769 }
770
771 fn link_rlib(&mut self, lib: &Path) {
772 self.add_object(lib);
773 }
774
775 fn position_independent_executable(&mut self) {
776 // noop
777 }
778
779 fn no_position_independent_executable(&mut self) {
780 // noop
781 }
782
783 fn full_relro(&mut self) {
784 // noop
785 }
786
787 fn partial_relro(&mut self) {
788 // noop
789 }
790
791 fn no_relro(&mut self) {
792 // noop
793 }
794
795 fn args(&mut self, args: &[String]) {
796 self.cmd.args(args);
797 }
798
799 fn framework_path(&mut self, _path: &Path) {
800 bug!("frameworks are not supported on Emscripten")
801 }
802
803 fn link_framework(&mut self, _framework: &str) {
804 bug!("frameworks are not supported on Emscripten")
805 }
806
807 fn gc_sections(&mut self, _keep_metadata: bool) {
808 // noop
809 }
810
811 fn optimize(&mut self) {
812 // Emscripten performs own optimizations
813 self.cmd.arg(match self.sess.opts.optimize {
814 OptLevel::No => "-O0",
815 OptLevel::Less => "-O1",
816 OptLevel::Default => "-O2",
817 OptLevel::Aggressive => "-O3",
818 OptLevel::Size => "-Os",
819 OptLevel::SizeMin => "-Oz"
820 });
821 // Unusable until https://github.com/rust-lang/rust/issues/38454 is resolved
822 self.cmd.args(&["--memory-init-file", "0"]);
823 }
824
825 fn pgo_gen(&mut self) {
826 // noop, but maybe we need something like the gnu linker?
827 }
828
829 fn debuginfo(&mut self) {
830 // Preserve names or generate source maps depending on debug info
831 self.cmd.arg(match self.sess.opts.debuginfo {
832 DebugInfo::None => "-g0",
833 DebugInfo::Limited => "-g3",
834 DebugInfo::Full => "-g4"
835 });
836 }
837
838 fn no_default_libraries(&mut self) {
839 self.cmd.args(&["-s", "DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]"]);
840 }
841
842 fn build_dylib(&mut self, _out_filename: &Path) {
843 bug!("building dynamic library is unsupported on Emscripten")
844 }
845
846 fn build_static_executable(&mut self) {
847 // noop
848 }
849
850 fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
851 let symbols = &self.info.exports[&crate_type];
852
853 debug!("EXPORTED SYMBOLS:");
854
855 self.cmd.arg("-s");
856
857 let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
858 let mut encoded = String::new();
859
860 {
861 let mut encoder = json::Encoder::new(&mut encoded);
862 let res = encoder.emit_seq(symbols.len(), |encoder| {
863 for (i, sym) in symbols.iter().enumerate() {
864 encoder.emit_seq_elt(i, |encoder| {
865 encoder.emit_str(&("_".to_owned() + sym))
866 })?;
867 }
868 Ok(())
869 });
870 if let Err(e) = res {
871 self.sess.fatal(&format!("failed to encode exported symbols: {}", e));
872 }
873 }
874 debug!("{}", encoded);
875 arg.push(encoded);
876
877 self.cmd.arg(arg);
878 }
879
880 fn subsystem(&mut self, _subsystem: &str) {
881 // noop
882 }
883
884 fn finalize(&mut self) -> Command {
885 ::std::mem::replace(&mut self.cmd, Command::new(""))
886 }
887
888 // Appears not necessary on Emscripten
889 fn group_start(&mut self) {}
890 fn group_end(&mut self) {}
891
892 fn linker_plugin_lto(&mut self) {
893 // Do nothing
894 }
895 }
896
897 pub struct WasmLd<'a> {
898 cmd: Command,
899 sess: &'a Session,
900 info: &'a LinkerInfo,
901 }
902
903 impl<'a> WasmLd<'a> {
904 fn new(mut cmd: Command, sess: &'a Session, info: &'a LinkerInfo) -> WasmLd<'a> {
905 // If the atomics feature is enabled for wasm then we need a whole bunch
906 // of flags:
907 //
908 // * `--shared-memory` - the link won't even succeed without this, flags
909 // the one linear memory as `shared`
910 //
911 // * `--max-memory=1G` - when specifying a shared memory this must also
912 // be specified. We conservatively choose 1GB but users should be able
913 // to override this with `-C link-arg`.
914 //
915 // * `--import-memory` - it doesn't make much sense for memory to be
916 // exported in a threaded module because typically you're
917 // sharing memory and instantiating the module multiple times. As a
918 // result if it were exported then we'd just have no sharing.
919 //
920 // * `--passive-segments` - all memory segments should be passive to
921 // prevent each module instantiation from reinitializing memory.
922 //
923 // * `--export=__wasm_init_memory` - when using `--passive-segments` the
924 // linker will synthesize this function, and so we need to make sure
925 // that our usage of `--export` below won't accidentally cause this
926 // function to get deleted.
927 //
928 // * `--export=*tls*` - when `#[thread_local]` symbols are used these
929 // symbols are how the TLS segments are initialized and configured.
930 let atomics = sess.opts.cg.target_feature.contains("+atomics") ||
931 sess.target.target.options.features.contains("+atomics");
932 if atomics {
933 cmd.arg("--shared-memory");
934 cmd.arg("--max-memory=1073741824");
935 cmd.arg("--import-memory");
936 cmd.arg("--passive-segments");
937 cmd.arg("--export=__wasm_init_memory");
938 cmd.arg("--export=__wasm_init_tls");
939 cmd.arg("--export=__tls_size");
940 cmd.arg("--export=__tls_align");
941 cmd.arg("--export=__tls_base");
942 }
943 WasmLd { cmd, sess, info }
944 }
945 }
946
947 impl<'a> Linker for WasmLd<'a> {
948 fn link_dylib(&mut self, lib: &str) {
949 self.cmd.arg("-l").arg(lib);
950 }
951
952 fn link_staticlib(&mut self, lib: &str) {
953 self.cmd.arg("-l").arg(lib);
954 }
955
956 fn link_rlib(&mut self, lib: &Path) {
957 self.cmd.arg(lib);
958 }
959
960 fn include_path(&mut self, path: &Path) {
961 self.cmd.arg("-L").arg(path);
962 }
963
964 fn framework_path(&mut self, _path: &Path) {
965 panic!("frameworks not supported")
966 }
967
968 fn output_filename(&mut self, path: &Path) {
969 self.cmd.arg("-o").arg(path);
970 }
971
972 fn add_object(&mut self, path: &Path) {
973 self.cmd.arg(path);
974 }
975
976 fn position_independent_executable(&mut self) {
977 }
978
979 fn full_relro(&mut self) {
980 }
981
982 fn partial_relro(&mut self) {
983 }
984
985 fn no_relro(&mut self) {
986 }
987
988 fn build_static_executable(&mut self) {
989 }
990
991 fn args(&mut self, args: &[String]) {
992 self.cmd.args(args);
993 }
994
995 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
996 self.cmd.arg("-l").arg(lib);
997 }
998
999 fn link_framework(&mut self, _framework: &str) {
1000 panic!("frameworks not supported")
1001 }
1002
1003 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
1004 self.cmd.arg("-l").arg(lib);
1005 }
1006
1007 fn link_whole_rlib(&mut self, lib: &Path) {
1008 self.cmd.arg(lib);
1009 }
1010
1011 fn gc_sections(&mut self, _keep_metadata: bool) {
1012 self.cmd.arg("--gc-sections");
1013 }
1014
1015 fn optimize(&mut self) {
1016 self.cmd.arg(match self.sess.opts.optimize {
1017 OptLevel::No => "-O0",
1018 OptLevel::Less => "-O1",
1019 OptLevel::Default => "-O2",
1020 OptLevel::Aggressive => "-O3",
1021 // Currently LLD doesn't support `Os` and `Oz`, so pass through `O2`
1022 // instead.
1023 OptLevel::Size => "-O2",
1024 OptLevel::SizeMin => "-O2"
1025 });
1026 }
1027
1028 fn pgo_gen(&mut self) {
1029 }
1030
1031 fn debuginfo(&mut self) {
1032 }
1033
1034 fn no_default_libraries(&mut self) {
1035 }
1036
1037 fn build_dylib(&mut self, _out_filename: &Path) {
1038 self.cmd.arg("--no-entry");
1039 }
1040
1041 fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
1042 for sym in self.info.exports[&crate_type].iter() {
1043 self.cmd.arg("--export").arg(&sym);
1044 }
1045
1046 // LLD will hide these otherwise-internal symbols since our `--export`
1047 // list above is a whitelist of what to export. Various bits and pieces
1048 // of tooling use this, so be sure these symbols make their way out of
1049 // the linker as well.
1050 self.cmd.arg("--export=__heap_base");
1051 self.cmd.arg("--export=__data_end");
1052 }
1053
1054 fn subsystem(&mut self, _subsystem: &str) {
1055 }
1056
1057 fn no_position_independent_executable(&mut self) {
1058 }
1059
1060 fn finalize(&mut self) -> Command {
1061 ::std::mem::replace(&mut self.cmd, Command::new(""))
1062 }
1063
1064 // Not needed for now with LLD
1065 fn group_start(&mut self) {}
1066 fn group_end(&mut self) {}
1067
1068 fn linker_plugin_lto(&mut self) {
1069 // Do nothing for now
1070 }
1071 }
1072
1073 fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
1074 if let Some(ref exports) = tcx.sess.target.target.options.override_export_symbols {
1075 return exports.clone()
1076 }
1077
1078 let mut symbols = Vec::new();
1079
1080 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
1081 for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() {
1082 if level.is_below_threshold(export_threshold) {
1083 symbols.push(symbol.symbol_name(tcx).to_string());
1084 }
1085 }
1086
1087 let formats = tcx.sess.dependency_formats.borrow();
1088 let deps = formats[&crate_type].iter();
1089
1090 for (index, dep_format) in deps.enumerate() {
1091 let cnum = CrateNum::new(index + 1);
1092 // For each dependency that we are linking to statically ...
1093 if *dep_format == Linkage::Static {
1094 // ... we add its symbol list to our export list.
1095 for &(symbol, level) in tcx.exported_symbols(cnum).iter() {
1096 if level.is_below_threshold(export_threshold) {
1097 symbols.push(symbol.symbol_name(tcx).to_string());
1098 }
1099 }
1100 }
1101 }
1102
1103 symbols
1104 }
1105
1106 /// Much simplified and explicit CLI for the NVPTX linker. The linker operates
1107 /// with bitcode and uses LLVM backend to generate a PTX assembly.
1108 pub struct PtxLinker<'a> {
1109 cmd: Command,
1110 sess: &'a Session,
1111 }
1112
1113 impl<'a> Linker for PtxLinker<'a> {
1114 fn link_rlib(&mut self, path: &Path) {
1115 self.cmd.arg("--rlib").arg(path);
1116 }
1117
1118 fn link_whole_rlib(&mut self, path: &Path) {
1119 self.cmd.arg("--rlib").arg(path);
1120 }
1121
1122 fn include_path(&mut self, path: &Path) {
1123 self.cmd.arg("-L").arg(path);
1124 }
1125
1126 fn debuginfo(&mut self) {
1127 self.cmd.arg("--debug");
1128 }
1129
1130 fn add_object(&mut self, path: &Path) {
1131 self.cmd.arg("--bitcode").arg(path);
1132 }
1133
1134 fn args(&mut self, args: &[String]) {
1135 self.cmd.args(args);
1136 }
1137
1138 fn optimize(&mut self) {
1139 match self.sess.lto() {
1140 Lto::Thin | Lto::Fat | Lto::ThinLocal => {
1141 self.cmd.arg("-Olto");
1142 },
1143
1144 Lto::No => { },
1145 };
1146 }
1147
1148 fn output_filename(&mut self, path: &Path) {
1149 self.cmd.arg("-o").arg(path);
1150 }
1151
1152 fn finalize(&mut self) -> Command {
1153 // Provide the linker with fallback to internal `target-cpu`.
1154 self.cmd.arg("--fallback-arch").arg(match self.sess.opts.cg.target_cpu {
1155 Some(ref s) => s,
1156 None => &self.sess.target.target.options.cpu
1157 });
1158
1159 ::std::mem::replace(&mut self.cmd, Command::new(""))
1160 }
1161
1162 fn link_dylib(&mut self, _lib: &str) {
1163 panic!("external dylibs not supported")
1164 }
1165
1166 fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) {
1167 panic!("external dylibs not supported")
1168 }
1169
1170 fn link_staticlib(&mut self, _lib: &str) {
1171 panic!("staticlibs not supported")
1172 }
1173
1174 fn link_whole_staticlib(&mut self, _lib: &str, _search_path: &[PathBuf]) {
1175 panic!("staticlibs not supported")
1176 }
1177
1178 fn framework_path(&mut self, _path: &Path) {
1179 panic!("frameworks not supported")
1180 }
1181
1182 fn link_framework(&mut self, _framework: &str) {
1183 panic!("frameworks not supported")
1184 }
1185
1186 fn position_independent_executable(&mut self) {
1187 }
1188
1189 fn full_relro(&mut self) {
1190 }
1191
1192 fn partial_relro(&mut self) {
1193 }
1194
1195 fn no_relro(&mut self) {
1196 }
1197
1198 fn build_static_executable(&mut self) {
1199 }
1200
1201 fn gc_sections(&mut self, _keep_metadata: bool) {
1202 }
1203
1204 fn pgo_gen(&mut self) {
1205 }
1206
1207 fn no_default_libraries(&mut self) {
1208 }
1209
1210 fn build_dylib(&mut self, _out_filename: &Path) {
1211 }
1212
1213 fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType) {
1214 }
1215
1216 fn subsystem(&mut self, _subsystem: &str) {
1217 }
1218
1219 fn no_position_independent_executable(&mut self) {
1220 }
1221
1222 fn group_start(&mut self) {
1223 }
1224
1225 fn group_end(&mut self) {
1226 }
1227
1228 fn linker_plugin_lto(&mut self) {
1229 }
1230 }