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
::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 use std
::process
::Command
;
19 use context
::SharedCrateContext
;
20 use monomorphize
::Instance
;
23 use middle
::dependency_format
::Linkage
;
24 use rustc
::hir
::def_id
::CrateNum
;
26 use session
::config
::CrateType
;
29 /// For all the linkers we support, and information they might
30 /// need out of the shared crate context before we get rid of it.
31 pub struct LinkerInfo
{
32 exports
: HashMap
<CrateType
, Vec
<String
>>,
35 impl<'a
, 'tcx
> LinkerInfo
{
36 pub fn new(scx
: &SharedCrateContext
<'a
, 'tcx
>,
37 reachable
: &[String
]) -> LinkerInfo
{
39 exports
: scx
.sess().crate_types
.borrow().iter().map(|&c
| {
40 (c
, exported_symbols(scx
, reachable
, c
))
45 pub fn to_linker(&'a
self,
47 sess
: &'a Session
) -> Box
<Linker
+'a
> {
48 if sess
.target
.target
.options
.is_like_msvc
{
64 /// Linker abstraction used by back::link to build up the command to invoke a
67 /// This trait is the total list of requirements needed by `back::link` and
68 /// represents the meaning of each option being passed down. This trait is then
69 /// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
70 /// MSVC linker (e.g. `link.exe`) is being used.
72 fn link_dylib(&mut self, lib
: &str);
73 fn link_rust_dylib(&mut self, lib
: &str, path
: &Path
);
74 fn link_framework(&mut self, framework
: &str);
75 fn link_staticlib(&mut self, lib
: &str);
76 fn link_rlib(&mut self, lib
: &Path
);
77 fn link_whole_rlib(&mut self, lib
: &Path
);
78 fn link_whole_staticlib(&mut self, lib
: &str, search_path
: &[PathBuf
]);
79 fn include_path(&mut self, path
: &Path
);
80 fn framework_path(&mut self, path
: &Path
);
81 fn output_filename(&mut self, path
: &Path
);
82 fn add_object(&mut self, path
: &Path
);
83 fn gc_sections(&mut self, keep_metadata
: bool
);
84 fn position_independent_executable(&mut self);
85 fn optimize(&mut self);
86 fn debuginfo(&mut self);
87 fn no_default_libraries(&mut self);
88 fn build_dylib(&mut self, out_filename
: &Path
);
89 fn args(&mut self, args
: &[String
]);
90 fn hint_static(&mut self);
91 fn hint_dynamic(&mut self);
92 fn whole_archives(&mut self);
93 fn no_whole_archives(&mut self);
94 fn export_symbols(&mut self, tmpdir
: &Path
, crate_type
: CrateType
);
97 pub struct GnuLinker
<'a
> {
103 impl<'a
> GnuLinker
<'a
> {
104 fn takes_hints(&self) -> bool
{
105 !self.sess
.target
.target
.options
.is_like_osx
109 impl<'a
> Linker
for GnuLinker
<'a
> {
110 fn link_dylib(&mut self, lib
: &str) { self.cmd.arg("-l").arg(lib); }
111 fn link_staticlib(&mut self, lib
: &str) { self.cmd.arg("-l").arg(lib); }
112 fn link_rlib(&mut self, lib
: &Path
) { self.cmd.arg(lib); }
113 fn include_path(&mut self, path
: &Path
) { self.cmd.arg("-L").arg(path); }
114 fn framework_path(&mut self, path
: &Path
) { self.cmd.arg("-F").arg(path); }
115 fn output_filename(&mut self, path
: &Path
) { self.cmd.arg("-o").arg(path); }
116 fn add_object(&mut self, path
: &Path
) { self.cmd.arg(path); }
117 fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
118 fn args(&mut self, args
: &[String
]) { self.cmd.args(args); }
120 fn link_rust_dylib(&mut self, lib
: &str, _path
: &Path
) {
121 self.cmd
.arg("-l").arg(lib
);
124 fn link_framework(&mut self, framework
: &str) {
125 self.cmd
.arg("-framework").arg(framework
);
128 fn link_whole_staticlib(&mut self, lib
: &str, search_path
: &[PathBuf
]) {
129 let target
= &self.sess
.target
.target
;
130 if !target
.options
.is_like_osx
{
131 self.cmd
.arg("-Wl,--whole-archive")
133 .arg("-Wl,--no-whole-archive");
135 // -force_load is the OSX equivalent of --whole-archive, but it
136 // involves passing the full path to the library to link.
137 let mut v
= OsString
::from("-Wl,-force_load,");
138 v
.push(&archive
::find_library(lib
, search_path
, &self.sess
));
143 fn link_whole_rlib(&mut self, lib
: &Path
) {
144 if self.sess
.target
.target
.options
.is_like_osx
{
145 let mut v
= OsString
::from("-Wl,-force_load,");
149 self.cmd
.arg("-Wl,--whole-archive").arg(lib
)
150 .arg("-Wl,--no-whole-archive");
154 fn gc_sections(&mut self, keep_metadata
: bool
) {
155 // The dead_strip option to the linker specifies that functions and data
156 // unreachable by the entry point will be removed. This is quite useful
157 // with Rust's compilation model of compiling libraries at a time into
158 // one object file. For example, this brings hello world from 1.7MB to
161 // Note that this is done for both executables and dynamic libraries. We
162 // won't get much benefit from dylibs because LLVM will have already
163 // stripped away as much as it could. This has not been seen to impact
164 // link times negatively.
166 // -dead_strip can't be part of the pre_link_args because it's also used
167 // for partial linking when using multiple codegen units (-r). So we
169 if self.sess
.target
.target
.options
.is_like_osx
{
170 self.cmd
.arg("-Wl,-dead_strip");
171 } else if self.sess
.target
.target
.options
.is_like_solaris
{
172 self.cmd
.arg("-Wl,-z");
173 self.cmd
.arg("-Wl,ignore");
175 // If we're building a dylib, we don't use --gc-sections because LLVM
176 // has already done the best it can do, and we also don't want to
177 // eliminate the metadata. If we're building an executable, however,
178 // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
180 } else if !keep_metadata
{
181 self.cmd
.arg("-Wl,--gc-sections");
185 fn optimize(&mut self) {
186 if !self.sess
.target
.target
.options
.linker_is_gnu { return }
188 // GNU-style linkers support optimization with -O. GNU ld doesn't
189 // need a numeric argument, but other linkers do.
190 if self.sess
.opts
.optimize
== config
::OptLevel
::Default
||
191 self.sess
.opts
.optimize
== config
::OptLevel
::Aggressive
{
192 self.cmd
.arg("-Wl,-O1");
196 fn debuginfo(&mut self) {
197 // Don't do anything special here for GNU-style linkers.
200 fn no_default_libraries(&mut self) {
201 self.cmd
.arg("-nodefaultlibs");
204 fn build_dylib(&mut self, out_filename
: &Path
) {
205 // On mac we need to tell the linker to let this library be rpathed
206 if self.sess
.target
.target
.options
.is_like_osx
{
207 self.cmd
.args(&["-dynamiclib", "-Wl,-dylib"]);
209 if self.sess
.opts
.cg
.rpath
{
210 let mut v
= OsString
::from("-Wl,-install_name,@rpath/");
211 v
.push(out_filename
.file_name().unwrap());
215 self.cmd
.arg("-shared");
219 fn whole_archives(&mut self) {
220 if !self.takes_hints() { return }
221 self.cmd
.arg("-Wl,--whole-archive");
224 fn no_whole_archives(&mut self) {
225 if !self.takes_hints() { return }
226 self.cmd
.arg("-Wl,--no-whole-archive");
229 fn hint_static(&mut self) {
230 if !self.takes_hints() { return }
231 self.cmd
.arg("-Wl,-Bstatic");
234 fn hint_dynamic(&mut self) {
235 if !self.takes_hints() { return }
236 self.cmd
.arg("-Wl,-Bdynamic");
239 fn export_symbols(&mut self, tmpdir
: &Path
, crate_type
: CrateType
) {
240 // If we're compiling a dylib, then we let symbol visibility in object
241 // files to take care of whether they're exported or not.
243 // If we're compiling a cdylib, however, we manually create a list of
244 // exported symbols to ensure we don't expose any more. The object files
245 // have far more public symbols than we actually want to export, so we
246 // hide them all here.
247 if crate_type
== CrateType
::CrateTypeDylib
||
248 crate_type
== CrateType
::CrateTypeRustcMacro
{
252 let mut arg
= OsString
::new();
253 let path
= tmpdir
.join("list");
255 if self.sess
.target
.target
.options
.is_like_solaris
{
256 let res
= (|| -> io
::Result
<()> {
257 let mut f
= BufWriter
::new(File
::create(&path
)?
);
258 writeln
!(f
, "{{\n global:")?
;
259 for sym
in self.info
.exports
[&crate_type
].iter() {
260 writeln
!(f
, " {};", sym
)?
;
262 writeln
!(f
, "\n local:\n *;\n}};")?
;
265 if let Err(e
) = res
{
266 self.sess
.fatal(&format
!("failed to write version script: {}", e
));
272 let prefix
= if self.sess
.target
.target
.options
.is_like_osx
{
277 let res
= (|| -> io
::Result
<()> {
278 let mut f
= BufWriter
::new(File
::create(&path
)?
);
279 for sym
in self.info
.exports
[&crate_type
].iter() {
280 writeln
!(f
, "{}{}", prefix
, sym
)?
;
284 if let Err(e
) = res
{
285 self.sess
.fatal(&format
!("failed to write lib.def file: {}", e
));
287 if self.sess
.target
.target
.options
.is_like_osx
{
288 arg
.push("-Wl,-exported_symbols_list,");
290 arg
.push("-Wl,--retain-symbols-file=");
299 pub struct MsvcLinker
<'a
> {
300 cmd
: &'a
mut Command
,
305 impl<'a
> Linker
for MsvcLinker
<'a
> {
306 fn link_rlib(&mut self, lib
: &Path
) { self.cmd.arg(lib); }
307 fn add_object(&mut self, path
: &Path
) { self.cmd.arg(path); }
308 fn args(&mut self, args
: &[String
]) { self.cmd.args(args); }
310 fn build_dylib(&mut self, out_filename
: &Path
) {
311 self.cmd
.arg("/DLL");
312 let mut arg
: OsString
= "/IMPLIB:".into();
313 arg
.push(out_filename
.with_extension("dll.lib"));
317 fn gc_sections(&mut self, _keep_metadata
: bool
) {
318 self.cmd
.arg("/OPT:REF,ICF");
321 fn link_dylib(&mut self, lib
: &str) {
322 self.cmd
.arg(&format
!("{}.lib", lib
));
325 fn link_rust_dylib(&mut self, lib
: &str, path
: &Path
) {
326 // When producing a dll, the MSVC linker may not actually emit a
327 // `foo.lib` file if the dll doesn't actually export any symbols, so we
328 // check to see if the file is there and just omit linking to it if it's
330 let name
= format
!("{}.dll.lib", lib
);
331 if fs
::metadata(&path
.join(&name
)).is_ok() {
336 fn link_staticlib(&mut self, lib
: &str) {
337 self.cmd
.arg(&format
!("{}.lib", lib
));
340 fn position_independent_executable(&mut self) {
344 fn no_default_libraries(&mut self) {
345 // Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
346 // as there's been trouble in the past of linking the C++ standard
347 // library required by LLVM. This likely needs to happen one day, but
348 // in general Windows is also a more controlled environment than
349 // Unix, so it's not necessarily as critical that this be implemented.
351 // Note that there are also some licensing worries about statically
352 // linking some libraries which require a specific agreement, so it may
353 // not ever be possible for us to pass this flag.
356 fn include_path(&mut self, path
: &Path
) {
357 let mut arg
= OsString
::from("/LIBPATH:");
362 fn output_filename(&mut self, path
: &Path
) {
363 let mut arg
= OsString
::from("/OUT:");
368 fn framework_path(&mut self, _path
: &Path
) {
369 bug
!("frameworks are not supported on windows")
371 fn link_framework(&mut self, _framework
: &str) {
372 bug
!("frameworks are not supported on windows")
375 fn link_whole_staticlib(&mut self, lib
: &str, _search_path
: &[PathBuf
]) {
377 self.link_staticlib(lib
);
379 fn link_whole_rlib(&mut self, path
: &Path
) {
381 self.link_rlib(path
);
383 fn optimize(&mut self) {
384 // Needs more investigation of `/OPT` arguments
387 fn debuginfo(&mut self) {
388 // This will cause the Microsoft linker to generate a PDB file
389 // from the CodeView line tables in the object files.
390 self.cmd
.arg("/DEBUG");
393 fn whole_archives(&mut self) {
394 // hints not supported?
396 fn no_whole_archives(&mut self) {
397 // hints not supported?
400 // On windows static libraries are of the form `foo.lib` and dynamic
401 // libraries are not linked against directly, but rather through their
402 // import libraries also called `foo.lib`. As a result there's no
403 // possibility for a native library to appear both dynamically and
404 // statically in the same folder so we don't have to worry about hints like
405 // we do on Unix platforms.
406 fn hint_static(&mut self) {}
407 fn hint_dynamic(&mut self) {}
409 // Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
410 // export symbols from a dynamic library. When building a dynamic library,
411 // however, we're going to want some symbols exported, so this function
412 // generates a DEF file which lists all the symbols.
414 // The linker will read this `*.def` file and export all the symbols from
415 // the dynamic library. Note that this is not as simple as just exporting
416 // all the symbols in the current crate (as specified by `trans.reachable`)
417 // but rather we also need to possibly export the symbols of upstream
418 // crates. Upstream rlibs may be linked statically to this dynamic library,
419 // in which case they may continue to transitively be used and hence need
420 // their symbols exported.
421 fn export_symbols(&mut self,
423 crate_type
: CrateType
) {
424 let path
= tmpdir
.join("lib.def");
425 let res
= (|| -> io
::Result
<()> {
426 let mut f
= BufWriter
::new(File
::create(&path
)?
);
428 // Start off with the standard module name header and then go
429 // straight to exports.
430 writeln
!(f
, "LIBRARY")?
;
431 writeln
!(f
, "EXPORTS")?
;
432 for symbol
in self.info
.exports
[&crate_type
].iter() {
433 writeln
!(f
, " {}", symbol
)?
;
437 if let Err(e
) = res
{
438 self.sess
.fatal(&format
!("failed to write lib.def file: {}", e
));
440 let mut arg
= OsString
::from("/DEF:");
446 fn exported_symbols(scx
: &SharedCrateContext
,
447 reachable
: &[String
],
448 crate_type
: CrateType
)
450 // See explanation in GnuLinker::export_symbols, for
451 // why we don't ever need dylib symbols on non-MSVC.
452 if crate_type
== CrateType
::CrateTypeDylib
||
453 crate_type
== CrateType
::CrateTypeRustcMacro
{
454 if !scx
.sess().target
.target
.options
.is_like_msvc
{
459 let mut symbols
= reachable
.to_vec();
461 // If we're producing anything other than a dylib then the `reachable` array
462 // above is the exhaustive set of symbols we should be exporting.
464 // For dylibs, however, we need to take a look at how all upstream crates
465 // are linked into this dynamic library. For all statically linked
466 // libraries we take all their reachable symbols and emit them as well.
467 if crate_type
!= CrateType
::CrateTypeDylib
{
471 let cstore
= &scx
.sess().cstore
;
472 let formats
= scx
.sess().dependency_formats
.borrow();
473 let deps
= formats
[&crate_type
].iter();
474 symbols
.extend(deps
.enumerate().filter_map(|(i
, f
)| {
475 if *f
== Linkage
::Static
{
476 Some(CrateNum
::new(i
+ 1))
481 cstore
.reachable_ids(cnum
)
482 }).map(|did
| -> String
{
483 Instance
::mono(scx
, did
).symbol_name(scx
)