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.
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.
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}
;
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
,
26 use rustc
::ty
::TyCtxt
;
27 use rustc_target
::spec
::{LinkerFlavor, LldFlavor}
;
28 use serialize
::{json, Encoder}
;
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
>>,
37 pub fn new(tcx
: TyCtxt
) -> LinkerInfo
{
39 exports
: tcx
.sess
.crate_types
.borrow().iter().map(|&c
| {
40 (c
, exported_symbols(tcx
, c
))
45 pub fn to_linker
<'a
>(&'a
self,
47 sess
: &'a Session
) -> Box
<Linker
+'a
> {
48 match sess
.linker_flavor() {
49 LinkerFlavor
::Lld(LldFlavor
::Link
) |
50 LinkerFlavor
::Msvc
=> {
64 LinkerFlavor
::Gcc
=> {
74 LinkerFlavor
::Lld(LldFlavor
::Ld
) |
75 LinkerFlavor
::Lld(LldFlavor
::Ld64
) |
86 LinkerFlavor
::Lld(LldFlavor
::Wasm
) => {
95 /// Linker abstraction used by back::link to build up the command to invoke a
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.
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
;
136 pub struct GccLinker
<'a
> {
139 info
: &'a LinkerInfo
,
140 hinted_static
: bool
, // Keeps track of the current hinting mode.
145 impl<'a
> GccLinker
<'a
> {
146 /// Argument that must be passed *directly* to the linker
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
>
153 let mut os
= OsString
::from("-Wl,");
154 os
.push(arg
.as_ref());
162 fn takes_hints(&self) -> bool
{
163 !self.sess
.target
.target
.options
.is_like_osx
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;
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;
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); }
203 fn link_rust_dylib(&mut self, lib
: &str, _path
: &Path
) {
205 self.cmd
.arg("-l").arg(lib
);
208 fn link_framework(&mut self, framework
: &str) {
210 self.cmd
.arg("-framework").arg(framework
);
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
219 fn link_whole_staticlib(&mut self, lib
: &str, search_path
: &[PathBuf
]) {
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");
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
));
234 fn link_whole_rlib(&mut self, lib
: &Path
) {
236 if self.sess
.target
.target
.options
.is_like_osx
{
237 let mut v
= OsString
::from("-force_load,");
241 self.linker_arg("--whole-archive").cmd
.arg(lib
);
242 self.linker_arg("--no-whole-archive");
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
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.
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
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");
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%
272 } else if !keep_metadata
{
273 self.linker_arg("--gc-sections");
277 fn optimize(&mut self) {
278 if !self.sess
.target
.target
.options
.linker_is_gnu { return }
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");
288 fn pgo_gen(&mut self) {
289 if !self.sess
.target
.target
.options
.linker_is_gnu { return }
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.
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
298 // See https://reviews.llvm.org/D14033 and https://reviews.llvm.org/D14030.
300 // Though it may be worth to try to revert those changes upstream, since
301 // the overhead of the initialization should be minor.
303 self.cmd
.arg("__llvm_profile_runtime");
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"); }
,
321 fn no_default_libraries(&mut self) {
323 self.cmd
.arg("-nodefaultlibs");
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");
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());
344 self.cmd
.arg("-shared");
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.
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
{
361 let mut arg
= OsString
::new();
362 let path
= tmpdir
.join("list");
364 debug
!("EXPORTED SYMBOLS:");
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() {
372 writeln
!(f
, "_{}", sym
)?
;
376 if let Err(e
) = res
{
377 self.sess
.fatal(&format
!("failed to write lib.def file: {}", e
));
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() {
386 writeln
!(f
, " {};", sym
)?
;
388 writeln
!(f
, "\n local:\n *;\n}};")?
;
391 if let Err(e
) = res
{
392 self.sess
.fatal(&format
!("failed to write version script: {}", e
));
396 if self.sess
.target
.target
.options
.is_like_osx
{
400 arg
.push("-exported_symbols_list,");
401 } else if self.sess
.target
.target
.options
.is_like_solaris
{
410 arg
.push("--version-script=");
417 fn subsystem(&mut self, subsystem
: &str) {
418 self.linker_arg(&format
!("--subsystem,{}", subsystem
));
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
);
428 fn group_start(&mut self) {
429 if !self.sess
.target
.target
.options
.is_like_osx
{
430 self.linker_arg("--start-group");
434 fn group_end(&mut self) {
435 if !self.sess
.target
.target
.options
.is_like_osx
{
436 self.linker_arg("--end-group");
440 fn cross_lang_lto(&mut self) {
441 match self.sess
.opts
.debugging_opts
.cross_lang_lto
{
442 CrossLangLto
::Disabled
|
443 CrossLangLto
::NoLink
=> {
446 CrossLangLto
::LinkerPlugin(ref path
) => {
447 self.linker_arg(&format
!("-plugin={}", path
.display()));
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",
458 self.linker_arg(&format
!("-plugin-opt={}", opt_level
));
459 self.linker_arg(&format
!("-plugin-opt=mcpu={}", self.sess
.target_cpu()));
461 match self.sess
.opts
.cg
.lto
{
463 config
::Lto
::ThinLocal
=> {
464 self.linker_arg(&format
!("-plugin-opt=thin"));
469 // default to regular LTO
477 pub struct MsvcLinker
<'a
> {
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); }
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"));
495 fn build_static_executable(&mut self) {
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");
506 // It is necessary to specify NOICF here, because /OPT:REF
507 // implies ICF by default.
508 self.cmd
.arg("/OPT:REF,NOICF");
512 fn link_dylib(&mut self, lib
: &str) {
513 self.cmd
.arg(&format
!("{}.lib", lib
));
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
521 let name
= format
!("{}.dll.lib", lib
);
522 if fs
::metadata(&path
.join(&name
)).is_ok() {
527 fn link_staticlib(&mut self, lib
: &str) {
528 self.cmd
.arg(&format
!("{}.lib", lib
));
531 fn position_independent_executable(&mut self) {
535 fn no_position_independent_executable(&mut self) {
539 fn full_relro(&mut self) {
543 fn partial_relro(&mut self) {
547 fn no_relro(&mut self) {
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.
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.
563 fn include_path(&mut self, path
: &Path
) {
564 let mut arg
= OsString
::from("/LIBPATH:");
569 fn output_filename(&mut self, path
: &Path
) {
570 let mut arg
= OsString
::from("/OUT:");
575 fn framework_path(&mut self, _path
: &Path
) {
576 bug
!("frameworks are not supported on windows")
578 fn link_framework(&mut self, _framework
: &str) {
579 bug
!("frameworks are not supported on windows")
582 fn link_whole_staticlib(&mut self, lib
: &str, _search_path
: &[PathBuf
]) {
584 self.link_staticlib(lib
);
586 fn link_whole_rlib(&mut self, path
: &Path
) {
588 self.link_rlib(path
);
590 fn optimize(&mut self) {
591 // Needs more investigation of `/OPT` arguments
594 fn pgo_gen(&mut self) {
595 // Nothing needed here.
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");
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");
619 for entry
in natvis_dir
{
622 let path
= entry
.path();
623 if path
.extension() == Some("natvis".as_ref()) {
624 let mut arg
= OsString
::from("/NATVIS:");
630 self.sess
.warn(&format
!("error enumerating natvis directory: {}", err
));
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.
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,
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
)?
);
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
)?
;
666 if let Err(e
) = res
{
667 self.sess
.fatal(&format
!("failed to write lib.def file: {}", e
));
669 let mut arg
= OsString
::from("/DEF:");
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
));
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.
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
692 // For more information see RFC #1665
693 if subsystem
== "windows" {
694 self.cmd
.arg("/ENTRY:mainCRTStartup");
698 fn finalize(&mut self) -> Command
{
699 let mut cmd
= Command
::new("");
700 ::std
::mem
::swap(&mut cmd
, &mut self.cmd
);
704 // MSVC doesn't need group indicators
705 fn group_start(&mut self) {}
706 fn group_end(&mut self) {}
708 fn cross_lang_lto(&mut self) {
713 pub struct EmLinker
<'a
> {
719 impl<'a
> Linker
for EmLinker
<'a
> {
720 fn include_path(&mut self, path
: &Path
) {
721 self.cmd
.arg("-L").arg(path
);
724 fn link_staticlib(&mut self, lib
: &str) {
725 self.cmd
.arg("-l").arg(lib
);
728 fn output_filename(&mut self, path
: &Path
) {
729 self.cmd
.arg("-o").arg(path
);
732 fn add_object(&mut self, path
: &Path
) {
736 fn link_dylib(&mut self, lib
: &str) {
737 // Emscripten always links statically
738 self.link_staticlib(lib
);
741 fn link_whole_staticlib(&mut self, lib
: &str, _search_path
: &[PathBuf
]) {
743 self.link_staticlib(lib
);
746 fn link_whole_rlib(&mut self, lib
: &Path
) {
751 fn link_rust_dylib(&mut self, lib
: &str, _path
: &Path
) {
752 self.link_dylib(lib
);
755 fn link_rlib(&mut self, lib
: &Path
) {
756 self.add_object(lib
);
759 fn position_independent_executable(&mut self) {
763 fn no_position_independent_executable(&mut self) {
767 fn full_relro(&mut self) {
771 fn partial_relro(&mut self) {
775 fn no_relro(&mut self) {
779 fn args(&mut self, args
: &[String
]) {
783 fn framework_path(&mut self, _path
: &Path
) {
784 bug
!("frameworks are not supported on Emscripten")
787 fn link_framework(&mut self, _framework
: &str) {
788 bug
!("frameworks are not supported on Emscripten")
791 fn gc_sections(&mut self, _keep_metadata
: bool
) {
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"
805 // Unusable until https://github.com/rust-lang/rust/issues/38454 is resolved
806 self.cmd
.args(&["--memory-init-file", "0"]);
809 fn pgo_gen(&mut self) {
810 // noop, but maybe we need something like the gnu linker?
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"
822 fn no_default_libraries(&mut self) {
823 self.cmd
.args(&["-s", "DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]"]);
826 fn build_dylib(&mut self, _out_filename
: &Path
) {
827 bug
!("building dynamic library is unsupported on Emscripten")
830 fn build_static_executable(&mut self) {
834 fn export_symbols(&mut self, _tmpdir
: &Path
, crate_type
: CrateType
) {
835 let symbols
= &self.info
.exports
[&crate_type
];
837 debug
!("EXPORTED SYMBOLS:");
841 let mut arg
= OsString
::from("EXPORTED_FUNCTIONS=");
842 let mut encoded
= String
::new();
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
))
854 if let Err(e
) = res
{
855 self.sess
.fatal(&format
!("failed to encode exported symbols: {}", e
));
858 debug
!("{}", encoded
);
864 fn subsystem(&mut self, _subsystem
: &str) {
868 fn finalize(&mut self) -> Command
{
869 let mut cmd
= Command
::new("");
870 ::std
::mem
::swap(&mut cmd
, &mut self.cmd
);
874 // Appears not necessary on Emscripten
875 fn group_start(&mut self) {}
876 fn group_end(&mut self) {}
878 fn cross_lang_lto(&mut self) {
883 fn exported_symbols(tcx
: TyCtxt
, crate_type
: CrateType
) -> Vec
<String
> {
884 let mut symbols
= Vec
::new();
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());
893 let formats
= tcx
.sess
.dependency_formats
.borrow();
894 let deps
= formats
[&crate_type
].iter();
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());
916 impl Linker
for WasmLd
{
917 fn link_dylib(&mut self, lib
: &str) {
918 self.cmd
.arg("-l").arg(lib
);
921 fn link_staticlib(&mut self, lib
: &str) {
922 self.cmd
.arg("-l").arg(lib
);
925 fn link_rlib(&mut self, lib
: &Path
) {
929 fn include_path(&mut self, path
: &Path
) {
930 self.cmd
.arg("-L").arg(path
);
933 fn framework_path(&mut self, _path
: &Path
) {
934 panic
!("frameworks not supported")
937 fn output_filename(&mut self, path
: &Path
) {
938 self.cmd
.arg("-o").arg(path
);
941 fn add_object(&mut self, path
: &Path
) {
945 fn position_independent_executable(&mut self) {
948 fn full_relro(&mut self) {
951 fn partial_relro(&mut self) {
954 fn no_relro(&mut self) {
957 fn build_static_executable(&mut self) {
960 fn args(&mut self, args
: &[String
]) {
964 fn link_rust_dylib(&mut self, lib
: &str, _path
: &Path
) {
965 self.cmd
.arg("-l").arg(lib
);
968 fn link_framework(&mut self, _framework
: &str) {
969 panic
!("frameworks not supported")
972 fn link_whole_staticlib(&mut self, lib
: &str, _search_path
: &[PathBuf
]) {
973 self.cmd
.arg("-l").arg(lib
);
976 fn link_whole_rlib(&mut self, lib
: &Path
) {
980 fn gc_sections(&mut self, _keep_metadata
: bool
) {
983 fn optimize(&mut self) {
986 fn pgo_gen(&mut self) {
989 fn debuginfo(&mut self) {
992 fn no_default_libraries(&mut self) {
995 fn build_dylib(&mut self, _out_filename
: &Path
) {
998 fn export_symbols(&mut self, _tmpdir
: &Path
, _crate_type
: CrateType
) {
1001 fn subsystem(&mut self, _subsystem
: &str) {
1004 fn no_position_independent_executable(&mut self) {
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");
1013 self.cmd
.arg("-z").arg("stack-size=1048576");
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");
1022 // For now we just never have an entry symbol
1023 self.cmd
.arg("--no-entry");
1025 let mut cmd
= Command
::new("");
1026 ::std
::mem
::swap(&mut cmd
, &mut self.cmd
);
1030 // Not needed for now with LLD
1031 fn group_start(&mut self) {}
1032 fn group_end(&mut self) {}
1034 fn cross_lang_lto(&mut self) {
1035 // Do nothing for now