]> git.proxmox.com Git - rustc.git/blob - src/librustc_codegen_llvm/back/linker.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / librustc_codegen_llvm / back / linker.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::collections::HashMap;
12 use std::ffi::{OsStr, OsString};
13 use std::fs::{self, File};
14 use std::io::prelude::*;
15 use std::io::{self, BufWriter};
16 use std::path::{Path, PathBuf};
17
18 use back::archive;
19 use back::command::Command;
20 use back::symbol_export;
21 use rustc::hir::def_id::{LOCAL_CRATE, CrateNum};
22 use rustc::middle::dependency_format::Linkage;
23 use rustc::session::Session;
24 use rustc::session::config::{self, CrateType, OptLevel, DebugInfoLevel,
25 CrossLangLto};
26 use rustc::ty::TyCtxt;
27 use rustc_target::spec::{LinkerFlavor, LldFlavor};
28 use serialize::{json, Encoder};
29
30 /// For all the linkers we support, and information they might
31 /// need out of the shared crate context before we get rid of it.
32 pub struct LinkerInfo {
33 exports: HashMap<CrateType, Vec<String>>,
34 }
35
36 impl LinkerInfo {
37 pub fn new(tcx: TyCtxt) -> LinkerInfo {
38 LinkerInfo {
39 exports: tcx.sess.crate_types.borrow().iter().map(|&c| {
40 (c, exported_symbols(tcx, c))
41 }).collect(),
42 }
43 }
44
45 pub fn to_linker<'a>(&'a self,
46 cmd: Command,
47 sess: &'a Session) -> Box<Linker+'a> {
48 match sess.linker_flavor() {
49 LinkerFlavor::Lld(LldFlavor::Link) |
50 LinkerFlavor::Msvc => {
51 Box::new(MsvcLinker {
52 cmd,
53 sess,
54 info: self
55 }) as Box<Linker>
56 }
57 LinkerFlavor::Em => {
58 Box::new(EmLinker {
59 cmd,
60 sess,
61 info: self
62 }) as Box<Linker>
63 }
64 LinkerFlavor::Gcc => {
65 Box::new(GccLinker {
66 cmd,
67 sess,
68 info: self,
69 hinted_static: false,
70 is_ld: false,
71 }) as Box<Linker>
72 }
73
74 LinkerFlavor::Lld(LldFlavor::Ld) |
75 LinkerFlavor::Lld(LldFlavor::Ld64) |
76 LinkerFlavor::Ld => {
77 Box::new(GccLinker {
78 cmd,
79 sess,
80 info: self,
81 hinted_static: false,
82 is_ld: true,
83 }) as Box<Linker>
84 }
85
86 LinkerFlavor::Lld(LldFlavor::Wasm) => {
87 Box::new(WasmLd {
88 cmd,
89 }) as Box<Linker>
90 }
91 }
92 }
93 }
94
95 /// Linker abstraction used by back::link to build up the command to invoke a
96 /// linker.
97 ///
98 /// This trait is the total list of requirements needed by `back::link` and
99 /// represents the meaning of each option being passed down. This trait is then
100 /// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
101 /// MSVC linker (e.g. `link.exe`) is being used.
102 pub trait Linker {
103 fn link_dylib(&mut self, lib: &str);
104 fn link_rust_dylib(&mut self, lib: &str, path: &Path);
105 fn link_framework(&mut self, framework: &str);
106 fn link_staticlib(&mut self, lib: &str);
107 fn link_rlib(&mut self, lib: &Path);
108 fn link_whole_rlib(&mut self, lib: &Path);
109 fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]);
110 fn include_path(&mut self, path: &Path);
111 fn framework_path(&mut self, path: &Path);
112 fn output_filename(&mut self, path: &Path);
113 fn add_object(&mut self, path: &Path);
114 fn gc_sections(&mut self, keep_metadata: bool);
115 fn position_independent_executable(&mut self);
116 fn no_position_independent_executable(&mut self);
117 fn full_relro(&mut self);
118 fn partial_relro(&mut self);
119 fn no_relro(&mut self);
120 fn optimize(&mut self);
121 fn pgo_gen(&mut self);
122 fn debuginfo(&mut self);
123 fn no_default_libraries(&mut self);
124 fn build_dylib(&mut self, out_filename: &Path);
125 fn build_static_executable(&mut self);
126 fn args(&mut self, args: &[String]);
127 fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
128 fn subsystem(&mut self, subsystem: &str);
129 fn group_start(&mut self);
130 fn group_end(&mut self);
131 fn cross_lang_lto(&mut self);
132 // Should have been finalize(self), but we don't support self-by-value on trait objects (yet?).
133 fn finalize(&mut self) -> Command;
134 }
135
136 pub struct GccLinker<'a> {
137 cmd: Command,
138 sess: &'a Session,
139 info: &'a LinkerInfo,
140 hinted_static: bool, // Keeps track of the current hinting mode.
141 // Link as ld
142 is_ld: bool,
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 !self.sess.target.target.options.is_like_osx
164 }
165
166 // Some platforms take hints about whether a library is static or dynamic.
167 // For those that support this, we ensure we pass the option if the library
168 // was flagged "static" (most defaults are dynamic) to ensure that if
169 // libfoo.a and libfoo.so both exist that the right one is chosen.
170 fn hint_static(&mut self) {
171 if !self.takes_hints() { return }
172 if !self.hinted_static {
173 self.linker_arg("-Bstatic");
174 self.hinted_static = true;
175 }
176 }
177
178 fn hint_dynamic(&mut self) {
179 if !self.takes_hints() { return }
180 if self.hinted_static {
181 self.linker_arg("-Bdynamic");
182 self.hinted_static = false;
183 }
184 }
185 }
186
187 impl<'a> Linker for GccLinker<'a> {
188 fn link_dylib(&mut self, lib: &str) { self.hint_dynamic(); self.cmd.arg("-l").arg(lib); }
189 fn link_staticlib(&mut self, lib: &str) { self.hint_static(); self.cmd.arg("-l").arg(lib); }
190 fn link_rlib(&mut self, lib: &Path) { self.hint_static(); self.cmd.arg(lib); }
191 fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); }
192 fn framework_path(&mut self, path: &Path) { self.cmd.arg("-F").arg(path); }
193 fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
194 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
195 fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
196 fn no_position_independent_executable(&mut self) { self.cmd.arg("-no-pie"); }
197 fn full_relro(&mut self) { self.linker_arg("-z,relro,-z,now"); }
198 fn partial_relro(&mut self) { self.linker_arg("-z,relro"); }
199 fn no_relro(&mut self) { self.linker_arg("-z,norelro"); }
200 fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
201 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
202
203 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
204 self.hint_dynamic();
205 self.cmd.arg("-l").arg(lib);
206 }
207
208 fn link_framework(&mut self, framework: &str) {
209 self.hint_dynamic();
210 self.cmd.arg("-framework").arg(framework);
211 }
212
213 // Here we explicitly ask that the entire archive is included into the
214 // result artifact. For more details see #15460, but the gist is that
215 // the linker will strip away any unused objects in the archive if we
216 // don't otherwise explicitly reference them. This can occur for
217 // libraries which are just providing bindings, libraries with generic
218 // functions, etc.
219 fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
220 self.hint_static();
221 let target = &self.sess.target.target;
222 if !target.options.is_like_osx {
223 self.linker_arg("--whole-archive").cmd.arg("-l").arg(lib);
224 self.linker_arg("--no-whole-archive");
225 } else {
226 // -force_load is the macOS equivalent of --whole-archive, but it
227 // involves passing the full path to the library to link.
228 let mut v = OsString::from("-force_load,");
229 v.push(&archive::find_library(lib, search_path, &self.sess));
230 self.linker_arg(&v);
231 }
232 }
233
234 fn link_whole_rlib(&mut self, lib: &Path) {
235 self.hint_static();
236 if self.sess.target.target.options.is_like_osx {
237 let mut v = OsString::from("-force_load,");
238 v.push(lib);
239 self.linker_arg(&v);
240 } else {
241 self.linker_arg("--whole-archive").cmd.arg(lib);
242 self.linker_arg("--no-whole-archive");
243 }
244 }
245
246 fn gc_sections(&mut self, keep_metadata: bool) {
247 // The dead_strip option to the linker specifies that functions and data
248 // unreachable by the entry point will be removed. This is quite useful
249 // with Rust's compilation model of compiling libraries at a time into
250 // one object file. For example, this brings hello world from 1.7MB to
251 // 458K.
252 //
253 // Note that this is done for both executables and dynamic libraries. We
254 // won't get much benefit from dylibs because LLVM will have already
255 // stripped away as much as it could. This has not been seen to impact
256 // link times negatively.
257 //
258 // -dead_strip can't be part of the pre_link_args because it's also used
259 // for partial linking when using multiple codegen units (-r). So we
260 // insert it here.
261 if self.sess.target.target.options.is_like_osx {
262 self.linker_arg("-dead_strip");
263 } else if self.sess.target.target.options.is_like_solaris {
264 self.linker_arg("-z");
265 self.linker_arg("ignore");
266
267 // If we're building a dylib, we don't use --gc-sections because LLVM
268 // has already done the best it can do, and we also don't want to
269 // eliminate the metadata. If we're building an executable, however,
270 // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
271 // reduction.
272 } else if !keep_metadata {
273 self.linker_arg("--gc-sections");
274 }
275 }
276
277 fn optimize(&mut self) {
278 if !self.sess.target.target.options.linker_is_gnu { return }
279
280 // GNU-style linkers support optimization with -O. GNU ld doesn't
281 // need a numeric argument, but other linkers do.
282 if self.sess.opts.optimize == config::OptLevel::Default ||
283 self.sess.opts.optimize == config::OptLevel::Aggressive {
284 self.linker_arg("-O1");
285 }
286 }
287
288 fn pgo_gen(&mut self) {
289 if !self.sess.target.target.options.linker_is_gnu { return }
290
291 // If we're doing PGO generation stuff and on a GNU-like linker, use the
292 // "-u" flag to properly pull in the profiler runtime bits.
293 //
294 // This is because LLVM otherwise won't add the needed initialization
295 // for us on Linux (though the extra flag should be harmless if it
296 // does).
297 //
298 // See https://reviews.llvm.org/D14033 and https://reviews.llvm.org/D14030.
299 //
300 // Though it may be worth to try to revert those changes upstream, since
301 // the overhead of the initialization should be minor.
302 self.cmd.arg("-u");
303 self.cmd.arg("__llvm_profile_runtime");
304 }
305
306 fn debuginfo(&mut self) {
307 match self.sess.opts.debuginfo {
308 DebugInfoLevel::NoDebugInfo => {
309 // If we are building without debuginfo enabled and we were called with
310 // `-Zstrip-debuginfo-if-disabled=yes`, tell the linker to strip any debuginfo
311 // found when linking to get rid of symbols from libstd.
312 match self.sess.opts.debugging_opts.strip_debuginfo_if_disabled {
313 Some(true) => { self.linker_arg("-S"); },
314 _ => {},
315 }
316 },
317 _ => {},
318 };
319 }
320
321 fn no_default_libraries(&mut self) {
322 if !self.is_ld {
323 self.cmd.arg("-nodefaultlibs");
324 }
325 }
326
327 fn build_dylib(&mut self, out_filename: &Path) {
328 // On mac we need to tell the linker to let this library be rpathed
329 if self.sess.target.target.options.is_like_osx {
330 self.cmd.arg("-dynamiclib");
331 self.linker_arg("-dylib");
332
333 // Note that the `osx_rpath_install_name` option here is a hack
334 // purely to support rustbuild right now, we should get a more
335 // principled solution at some point to force the compiler to pass
336 // the right `-Wl,-install_name` with an `@rpath` in it.
337 if self.sess.opts.cg.rpath ||
338 self.sess.opts.debugging_opts.osx_rpath_install_name {
339 let mut v = OsString::from("-install_name,@rpath/");
340 v.push(out_filename.file_name().unwrap());
341 self.linker_arg(&v);
342 }
343 } else {
344 self.cmd.arg("-shared");
345 }
346 }
347
348 fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
349 // If we're compiling a dylib, then we let symbol visibility in object
350 // files to take care of whether they're exported or not.
351 //
352 // If we're compiling a cdylib, however, we manually create a list of
353 // exported symbols to ensure we don't expose any more. The object files
354 // have far more public symbols than we actually want to export, so we
355 // hide them all here.
356 if crate_type == CrateType::CrateTypeDylib ||
357 crate_type == CrateType::CrateTypeProcMacro {
358 return
359 }
360
361 let mut arg = OsString::new();
362 let path = tmpdir.join("list");
363
364 debug!("EXPORTED SYMBOLS:");
365
366 if self.sess.target.target.options.is_like_osx {
367 // Write a plain, newline-separated list of symbols
368 let res = (|| -> io::Result<()> {
369 let mut f = BufWriter::new(File::create(&path)?);
370 for sym in self.info.exports[&crate_type].iter() {
371 debug!(" _{}", sym);
372 writeln!(f, "_{}", sym)?;
373 }
374 Ok(())
375 })();
376 if let Err(e) = res {
377 self.sess.fatal(&format!("failed to write lib.def file: {}", e));
378 }
379 } else {
380 // Write an LD version script
381 let res = (|| -> io::Result<()> {
382 let mut f = BufWriter::new(File::create(&path)?);
383 writeln!(f, "{{\n global:")?;
384 for sym in self.info.exports[&crate_type].iter() {
385 debug!(" {};", sym);
386 writeln!(f, " {};", sym)?;
387 }
388 writeln!(f, "\n local:\n *;\n}};")?;
389 Ok(())
390 })();
391 if let Err(e) = res {
392 self.sess.fatal(&format!("failed to write version script: {}", e));
393 }
394 }
395
396 if self.sess.target.target.options.is_like_osx {
397 if !self.is_ld {
398 arg.push("-Wl,")
399 }
400 arg.push("-exported_symbols_list,");
401 } else if self.sess.target.target.options.is_like_solaris {
402 if !self.is_ld {
403 arg.push("-Wl,")
404 }
405 arg.push("-M,");
406 } else {
407 if !self.is_ld {
408 arg.push("-Wl,")
409 }
410 arg.push("--version-script=");
411 }
412
413 arg.push(&path);
414 self.cmd.arg(arg);
415 }
416
417 fn subsystem(&mut self, subsystem: &str) {
418 self.linker_arg(&format!("--subsystem,{}", subsystem));
419 }
420
421 fn finalize(&mut self) -> Command {
422 self.hint_dynamic(); // Reset to default before returning the composed command line.
423 let mut cmd = Command::new("");
424 ::std::mem::swap(&mut cmd, &mut self.cmd);
425 cmd
426 }
427
428 fn group_start(&mut self) {
429 if !self.sess.target.target.options.is_like_osx {
430 self.linker_arg("--start-group");
431 }
432 }
433
434 fn group_end(&mut self) {
435 if !self.sess.target.target.options.is_like_osx {
436 self.linker_arg("--end-group");
437 }
438 }
439
440 fn cross_lang_lto(&mut self) {
441 match self.sess.opts.debugging_opts.cross_lang_lto {
442 CrossLangLto::Disabled |
443 CrossLangLto::NoLink => {
444 // Nothing to do
445 }
446 CrossLangLto::LinkerPlugin(ref path) => {
447 self.linker_arg(&format!("-plugin={}", path.display()));
448
449 let opt_level = match self.sess.opts.optimize {
450 config::OptLevel::No => "O0",
451 config::OptLevel::Less => "O1",
452 config::OptLevel::Default => "O2",
453 config::OptLevel::Aggressive => "O3",
454 config::OptLevel::Size => "Os",
455 config::OptLevel::SizeMin => "Oz",
456 };
457
458 self.linker_arg(&format!("-plugin-opt={}", opt_level));
459 self.linker_arg(&format!("-plugin-opt=mcpu={}", self.sess.target_cpu()));
460
461 match self.sess.opts.cg.lto {
462 config::Lto::Thin |
463 config::Lto::ThinLocal => {
464 self.linker_arg(&format!("-plugin-opt=thin"));
465 }
466 config::Lto::Fat |
467 config::Lto::Yes |
468 config::Lto::No => {
469 // default to regular LTO
470 }
471 }
472 }
473 }
474 }
475 }
476
477 pub struct MsvcLinker<'a> {
478 cmd: Command,
479 sess: &'a Session,
480 info: &'a LinkerInfo
481 }
482
483 impl<'a> Linker for MsvcLinker<'a> {
484 fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
485 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
486 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
487
488 fn build_dylib(&mut self, out_filename: &Path) {
489 self.cmd.arg("/DLL");
490 let mut arg: OsString = "/IMPLIB:".into();
491 arg.push(out_filename.with_extension("dll.lib"));
492 self.cmd.arg(arg);
493 }
494
495 fn build_static_executable(&mut self) {
496 // noop
497 }
498
499 fn gc_sections(&mut self, _keep_metadata: bool) {
500 // MSVC's ICF (Identical COMDAT Folding) link optimization is
501 // slow for Rust and thus we disable it by default when not in
502 // optimization build.
503 if self.sess.opts.optimize != config::OptLevel::No {
504 self.cmd.arg("/OPT:REF,ICF");
505 } else {
506 // It is necessary to specify NOICF here, because /OPT:REF
507 // implies ICF by default.
508 self.cmd.arg("/OPT:REF,NOICF");
509 }
510 }
511
512 fn link_dylib(&mut self, lib: &str) {
513 self.cmd.arg(&format!("{}.lib", lib));
514 }
515
516 fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
517 // When producing a dll, the MSVC linker may not actually emit a
518 // `foo.lib` file if the dll doesn't actually export any symbols, so we
519 // check to see if the file is there and just omit linking to it if it's
520 // not present.
521 let name = format!("{}.dll.lib", lib);
522 if fs::metadata(&path.join(&name)).is_ok() {
523 self.cmd.arg(name);
524 }
525 }
526
527 fn link_staticlib(&mut self, lib: &str) {
528 self.cmd.arg(&format!("{}.lib", lib));
529 }
530
531 fn position_independent_executable(&mut self) {
532 // noop
533 }
534
535 fn no_position_independent_executable(&mut self) {
536 // noop
537 }
538
539 fn full_relro(&mut self) {
540 // noop
541 }
542
543 fn partial_relro(&mut self) {
544 // noop
545 }
546
547 fn no_relro(&mut self) {
548 // noop
549 }
550
551 fn no_default_libraries(&mut self) {
552 // Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
553 // as there's been trouble in the past of linking the C++ standard
554 // library required by LLVM. This likely needs to happen one day, but
555 // in general Windows is also a more controlled environment than
556 // Unix, so it's not necessarily as critical that this be implemented.
557 //
558 // Note that there are also some licensing worries about statically
559 // linking some libraries which require a specific agreement, so it may
560 // not ever be possible for us to pass this flag.
561 }
562
563 fn include_path(&mut self, path: &Path) {
564 let mut arg = OsString::from("/LIBPATH:");
565 arg.push(path);
566 self.cmd.arg(&arg);
567 }
568
569 fn output_filename(&mut self, path: &Path) {
570 let mut arg = OsString::from("/OUT:");
571 arg.push(path);
572 self.cmd.arg(&arg);
573 }
574
575 fn framework_path(&mut self, _path: &Path) {
576 bug!("frameworks are not supported on windows")
577 }
578 fn link_framework(&mut self, _framework: &str) {
579 bug!("frameworks are not supported on windows")
580 }
581
582 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
583 // not supported?
584 self.link_staticlib(lib);
585 }
586 fn link_whole_rlib(&mut self, path: &Path) {
587 // not supported?
588 self.link_rlib(path);
589 }
590 fn optimize(&mut self) {
591 // Needs more investigation of `/OPT` arguments
592 }
593
594 fn pgo_gen(&mut self) {
595 // Nothing needed here.
596 }
597
598 fn debuginfo(&mut self) {
599 // This will cause the Microsoft linker to generate a PDB file
600 // from the CodeView line tables in the object files.
601 self.cmd.arg("/DEBUG");
602
603 // This will cause the Microsoft linker to embed .natvis info into the the PDB file
604 let sysroot = self.sess.sysroot();
605 let natvis_dir_path = sysroot.join("lib\\rustlib\\etc");
606 if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
607 // LLVM 5.0.0's lld-link frontend doesn't yet recognize, and chokes
608 // on, the /NATVIS:... flags. LLVM 6 (or earlier) should at worst ignore
609 // them, eventually mooting this workaround, per this landed patch:
610 // https://github.com/llvm-mirror/lld/commit/27b9c4285364d8d76bb43839daa100
611 if let Some(ref linker_path) = self.sess.opts.cg.linker {
612 if let Some(linker_name) = Path::new(&linker_path).file_stem() {
613 if linker_name.to_str().unwrap().to_lowercase() == "lld-link" {
614 self.sess.warn("not embedding natvis: lld-link may not support the flag");
615 return;
616 }
617 }
618 }
619 for entry in natvis_dir {
620 match entry {
621 Ok(entry) => {
622 let path = entry.path();
623 if path.extension() == Some("natvis".as_ref()) {
624 let mut arg = OsString::from("/NATVIS:");
625 arg.push(path);
626 self.cmd.arg(arg);
627 }
628 },
629 Err(err) => {
630 self.sess.warn(&format!("error enumerating natvis directory: {}", err));
631 },
632 }
633 }
634 }
635 }
636
637 // Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
638 // export symbols from a dynamic library. When building a dynamic library,
639 // however, we're going to want some symbols exported, so this function
640 // generates a DEF file which lists all the symbols.
641 //
642 // The linker will read this `*.def` file and export all the symbols from
643 // the dynamic library. Note that this is not as simple as just exporting
644 // all the symbols in the current crate (as specified by `codegen.reachable`)
645 // but rather we also need to possibly export the symbols of upstream
646 // crates. Upstream rlibs may be linked statically to this dynamic library,
647 // in which case they may continue to transitively be used and hence need
648 // their symbols exported.
649 fn export_symbols(&mut self,
650 tmpdir: &Path,
651 crate_type: CrateType) {
652 let path = tmpdir.join("lib.def");
653 let res = (|| -> io::Result<()> {
654 let mut f = BufWriter::new(File::create(&path)?);
655
656 // Start off with the standard module name header and then go
657 // straight to exports.
658 writeln!(f, "LIBRARY")?;
659 writeln!(f, "EXPORTS")?;
660 for symbol in self.info.exports[&crate_type].iter() {
661 debug!(" _{}", symbol);
662 writeln!(f, " {}", symbol)?;
663 }
664 Ok(())
665 })();
666 if let Err(e) = res {
667 self.sess.fatal(&format!("failed to write lib.def file: {}", e));
668 }
669 let mut arg = OsString::from("/DEF:");
670 arg.push(path);
671 self.cmd.arg(&arg);
672 }
673
674 fn subsystem(&mut self, subsystem: &str) {
675 // Note that previous passes of the compiler validated this subsystem,
676 // so we just blindly pass it to the linker.
677 self.cmd.arg(&format!("/SUBSYSTEM:{}", subsystem));
678
679 // Windows has two subsystems we're interested in right now, the console
680 // and windows subsystems. These both implicitly have different entry
681 // points (starting symbols). The console entry point starts with
682 // `mainCRTStartup` and the windows entry point starts with
683 // `WinMainCRTStartup`. These entry points, defined in system libraries,
684 // will then later probe for either `main` or `WinMain`, respectively to
685 // start the application.
686 //
687 // In Rust we just always generate a `main` function so we want control
688 // to always start there, so we force the entry point on the windows
689 // subsystem to be `mainCRTStartup` to get everything booted up
690 // correctly.
691 //
692 // For more information see RFC #1665
693 if subsystem == "windows" {
694 self.cmd.arg("/ENTRY:mainCRTStartup");
695 }
696 }
697
698 fn finalize(&mut self) -> Command {
699 let mut cmd = Command::new("");
700 ::std::mem::swap(&mut cmd, &mut self.cmd);
701 cmd
702 }
703
704 // MSVC doesn't need group indicators
705 fn group_start(&mut self) {}
706 fn group_end(&mut self) {}
707
708 fn cross_lang_lto(&mut self) {
709 // Do nothing
710 }
711 }
712
713 pub struct EmLinker<'a> {
714 cmd: Command,
715 sess: &'a Session,
716 info: &'a LinkerInfo
717 }
718
719 impl<'a> Linker for EmLinker<'a> {
720 fn include_path(&mut self, path: &Path) {
721 self.cmd.arg("-L").arg(path);
722 }
723
724 fn link_staticlib(&mut self, lib: &str) {
725 self.cmd.arg("-l").arg(lib);
726 }
727
728 fn output_filename(&mut self, path: &Path) {
729 self.cmd.arg("-o").arg(path);
730 }
731
732 fn add_object(&mut self, path: &Path) {
733 self.cmd.arg(path);
734 }
735
736 fn link_dylib(&mut self, lib: &str) {
737 // Emscripten always links statically
738 self.link_staticlib(lib);
739 }
740
741 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
742 // not supported?
743 self.link_staticlib(lib);
744 }
745
746 fn link_whole_rlib(&mut self, lib: &Path) {
747 // not supported?
748 self.link_rlib(lib);
749 }
750
751 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
752 self.link_dylib(lib);
753 }
754
755 fn link_rlib(&mut self, lib: &Path) {
756 self.add_object(lib);
757 }
758
759 fn position_independent_executable(&mut self) {
760 // noop
761 }
762
763 fn no_position_independent_executable(&mut self) {
764 // noop
765 }
766
767 fn full_relro(&mut self) {
768 // noop
769 }
770
771 fn partial_relro(&mut self) {
772 // noop
773 }
774
775 fn no_relro(&mut self) {
776 // noop
777 }
778
779 fn args(&mut self, args: &[String]) {
780 self.cmd.args(args);
781 }
782
783 fn framework_path(&mut self, _path: &Path) {
784 bug!("frameworks are not supported on Emscripten")
785 }
786
787 fn link_framework(&mut self, _framework: &str) {
788 bug!("frameworks are not supported on Emscripten")
789 }
790
791 fn gc_sections(&mut self, _keep_metadata: bool) {
792 // noop
793 }
794
795 fn optimize(&mut self) {
796 // Emscripten performs own optimizations
797 self.cmd.arg(match self.sess.opts.optimize {
798 OptLevel::No => "-O0",
799 OptLevel::Less => "-O1",
800 OptLevel::Default => "-O2",
801 OptLevel::Aggressive => "-O3",
802 OptLevel::Size => "-Os",
803 OptLevel::SizeMin => "-Oz"
804 });
805 // Unusable until https://github.com/rust-lang/rust/issues/38454 is resolved
806 self.cmd.args(&["--memory-init-file", "0"]);
807 }
808
809 fn pgo_gen(&mut self) {
810 // noop, but maybe we need something like the gnu linker?
811 }
812
813 fn debuginfo(&mut self) {
814 // Preserve names or generate source maps depending on debug info
815 self.cmd.arg(match self.sess.opts.debuginfo {
816 DebugInfoLevel::NoDebugInfo => "-g0",
817 DebugInfoLevel::LimitedDebugInfo => "-g3",
818 DebugInfoLevel::FullDebugInfo => "-g4"
819 });
820 }
821
822 fn no_default_libraries(&mut self) {
823 self.cmd.args(&["-s", "DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]"]);
824 }
825
826 fn build_dylib(&mut self, _out_filename: &Path) {
827 bug!("building dynamic library is unsupported on Emscripten")
828 }
829
830 fn build_static_executable(&mut self) {
831 // noop
832 }
833
834 fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
835 let symbols = &self.info.exports[&crate_type];
836
837 debug!("EXPORTED SYMBOLS:");
838
839 self.cmd.arg("-s");
840
841 let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
842 let mut encoded = String::new();
843
844 {
845 let mut encoder = json::Encoder::new(&mut encoded);
846 let res = encoder.emit_seq(symbols.len(), |encoder| {
847 for (i, sym) in symbols.iter().enumerate() {
848 encoder.emit_seq_elt(i, |encoder| {
849 encoder.emit_str(&("_".to_string() + sym))
850 })?;
851 }
852 Ok(())
853 });
854 if let Err(e) = res {
855 self.sess.fatal(&format!("failed to encode exported symbols: {}", e));
856 }
857 }
858 debug!("{}", encoded);
859 arg.push(encoded);
860
861 self.cmd.arg(arg);
862 }
863
864 fn subsystem(&mut self, _subsystem: &str) {
865 // noop
866 }
867
868 fn finalize(&mut self) -> Command {
869 let mut cmd = Command::new("");
870 ::std::mem::swap(&mut cmd, &mut self.cmd);
871 cmd
872 }
873
874 // Appears not necessary on Emscripten
875 fn group_start(&mut self) {}
876 fn group_end(&mut self) {}
877
878 fn cross_lang_lto(&mut self) {
879 // Do nothing
880 }
881 }
882
883 fn exported_symbols(tcx: TyCtxt, crate_type: CrateType) -> Vec<String> {
884 let mut symbols = Vec::new();
885
886 let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
887 for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() {
888 if level.is_below_threshold(export_threshold) {
889 symbols.push(symbol.symbol_name(tcx).to_string());
890 }
891 }
892
893 let formats = tcx.sess.dependency_formats.borrow();
894 let deps = formats[&crate_type].iter();
895
896 for (index, dep_format) in deps.enumerate() {
897 let cnum = CrateNum::new(index + 1);
898 // For each dependency that we are linking to statically ...
899 if *dep_format == Linkage::Static {
900 // ... we add its symbol list to our export list.
901 for &(symbol, level) in tcx.exported_symbols(cnum).iter() {
902 if level.is_below_threshold(export_threshold) {
903 symbols.push(symbol.symbol_name(tcx).to_string());
904 }
905 }
906 }
907 }
908
909 symbols
910 }
911
912 pub struct WasmLd {
913 cmd: Command,
914 }
915
916 impl Linker for WasmLd {
917 fn link_dylib(&mut self, lib: &str) {
918 self.cmd.arg("-l").arg(lib);
919 }
920
921 fn link_staticlib(&mut self, lib: &str) {
922 self.cmd.arg("-l").arg(lib);
923 }
924
925 fn link_rlib(&mut self, lib: &Path) {
926 self.cmd.arg(lib);
927 }
928
929 fn include_path(&mut self, path: &Path) {
930 self.cmd.arg("-L").arg(path);
931 }
932
933 fn framework_path(&mut self, _path: &Path) {
934 panic!("frameworks not supported")
935 }
936
937 fn output_filename(&mut self, path: &Path) {
938 self.cmd.arg("-o").arg(path);
939 }
940
941 fn add_object(&mut self, path: &Path) {
942 self.cmd.arg(path);
943 }
944
945 fn position_independent_executable(&mut self) {
946 }
947
948 fn full_relro(&mut self) {
949 }
950
951 fn partial_relro(&mut self) {
952 }
953
954 fn no_relro(&mut self) {
955 }
956
957 fn build_static_executable(&mut self) {
958 }
959
960 fn args(&mut self, args: &[String]) {
961 self.cmd.args(args);
962 }
963
964 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
965 self.cmd.arg("-l").arg(lib);
966 }
967
968 fn link_framework(&mut self, _framework: &str) {
969 panic!("frameworks not supported")
970 }
971
972 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
973 self.cmd.arg("-l").arg(lib);
974 }
975
976 fn link_whole_rlib(&mut self, lib: &Path) {
977 self.cmd.arg(lib);
978 }
979
980 fn gc_sections(&mut self, _keep_metadata: bool) {
981 }
982
983 fn optimize(&mut self) {
984 }
985
986 fn pgo_gen(&mut self) {
987 }
988
989 fn debuginfo(&mut self) {
990 }
991
992 fn no_default_libraries(&mut self) {
993 }
994
995 fn build_dylib(&mut self, _out_filename: &Path) {
996 }
997
998 fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType) {
999 }
1000
1001 fn subsystem(&mut self, _subsystem: &str) {
1002 }
1003
1004 fn no_position_independent_executable(&mut self) {
1005 }
1006
1007 fn finalize(&mut self) -> Command {
1008 // There have been reports in the wild (rustwasm/wasm-bindgen#119) of
1009 // using threads causing weird hangs and bugs. Disable it entirely as
1010 // this isn't yet the bottleneck of compilation at all anyway.
1011 self.cmd.arg("--no-threads");
1012
1013 self.cmd.arg("-z").arg("stack-size=1048576");
1014
1015 // FIXME we probably shouldn't pass this but instead pass an explicit
1016 // whitelist of symbols we'll allow to be undefined. Unfortunately
1017 // though we can't handle symbols like `log10` that LLVM injects at a
1018 // super late date without actually parsing object files. For now let's
1019 // stick to this and hopefully fix it before stabilization happens.
1020 self.cmd.arg("--allow-undefined");
1021
1022 // For now we just never have an entry symbol
1023 self.cmd.arg("--no-entry");
1024
1025 let mut cmd = Command::new("");
1026 ::std::mem::swap(&mut cmd, &mut self.cmd);
1027 cmd
1028 }
1029
1030 // Not needed for now with LLD
1031 fn group_start(&mut self) {}
1032 fn group_end(&mut self) {}
1033
1034 fn cross_lang_lto(&mut self) {
1035 // Do nothing for now
1036 }
1037 }