]> git.proxmox.com Git - rustc.git/blame - src/librustc_trans/back/linker.rs
New upstream version 1.15.0+dfsg1
[rustc.git] / src / librustc_trans / back / linker.rs
CommitLineData
62682a34
SL
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
9e0c209e 11use std::collections::HashMap;
62682a34 12use std::ffi::OsString;
e9174d1e 13use std::fs::{self, File};
e9174d1e 14use std::io::prelude::*;
9e0c209e 15use std::io::{self, BufWriter};
62682a34
SL
16use std::path::{Path, PathBuf};
17use std::process::Command;
18
3157f602 19use context::SharedCrateContext;
3157f602 20
c1a9b12d 21use back::archive;
476ff2be 22use back::symbol_export::{self, ExportedSymbols};
e9174d1e 23use middle::dependency_format::Linkage;
476ff2be 24use rustc::hir::def_id::{LOCAL_CRATE, CrateNum};
62682a34 25use session::Session;
a7813a04 26use session::config::CrateType;
e9174d1e 27use session::config;
3157f602
XL
28
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.
31pub struct LinkerInfo {
9e0c209e 32 exports: HashMap<CrateType, Vec<String>>,
3157f602
XL
33}
34
35impl<'a, 'tcx> LinkerInfo {
36 pub fn new(scx: &SharedCrateContext<'a, 'tcx>,
476ff2be 37 exports: &ExportedSymbols) -> LinkerInfo {
3157f602 38 LinkerInfo {
9e0c209e 39 exports: scx.sess().crate_types.borrow().iter().map(|&c| {
476ff2be 40 (c, exported_symbols(scx, exports, c))
9e0c209e 41 }).collect(),
3157f602
XL
42 }
43 }
44
45 pub fn to_linker(&'a self,
46 cmd: &'a mut Command,
47 sess: &'a Session) -> Box<Linker+'a> {
48 if sess.target.target.options.is_like_msvc {
49 Box::new(MsvcLinker {
50 cmd: cmd,
51 sess: sess,
52 info: self
53 }) as Box<Linker>
54 } else {
55 Box::new(GnuLinker {
56 cmd: cmd,
57 sess: sess,
58 info: self
59 }) as Box<Linker>
60 }
61 }
62}
62682a34
SL
63
64/// Linker abstraction used by back::link to build up the command to invoke a
65/// linker.
66///
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.
71pub trait Linker {
72 fn link_dylib(&mut self, lib: &str);
c1a9b12d 73 fn link_rust_dylib(&mut self, lib: &str, path: &Path);
62682a34
SL
74 fn link_framework(&mut self, framework: &str);
75 fn link_staticlib(&mut self, lib: &str);
76 fn link_rlib(&mut self, lib: &Path);
c1a9b12d 77 fn link_whole_rlib(&mut self, lib: &Path);
62682a34
SL
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);
a7813a04 83 fn gc_sections(&mut self, keep_metadata: bool);
62682a34
SL
84 fn position_independent_executable(&mut self);
85 fn optimize(&mut self);
c1a9b12d 86 fn debuginfo(&mut self);
62682a34
SL
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);
3157f602 94 fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
c30ab7b3 95 fn subsystem(&mut self, subsystem: &str);
62682a34
SL
96}
97
98pub struct GnuLinker<'a> {
3157f602
XL
99 cmd: &'a mut Command,
100 sess: &'a Session,
101 info: &'a LinkerInfo
62682a34
SL
102}
103
104impl<'a> GnuLinker<'a> {
105 fn takes_hints(&self) -> bool {
106 !self.sess.target.target.options.is_like_osx
107 }
108}
109
110impl<'a> Linker for GnuLinker<'a> {
111 fn link_dylib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
112 fn link_staticlib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
113 fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
114 fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); }
115 fn framework_path(&mut self, path: &Path) { self.cmd.arg("-F").arg(path); }
116 fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
117 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
118 fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
119 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
120
c1a9b12d
SL
121 fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
122 self.cmd.arg("-l").arg(lib);
123 }
124
62682a34
SL
125 fn link_framework(&mut self, framework: &str) {
126 self.cmd.arg("-framework").arg(framework);
127 }
128
129 fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
130 let target = &self.sess.target.target;
131 if !target.options.is_like_osx {
132 self.cmd.arg("-Wl,--whole-archive")
133 .arg("-l").arg(lib)
134 .arg("-Wl,--no-whole-archive");
135 } else {
136 // -force_load is the OSX equivalent of --whole-archive, but it
137 // involves passing the full path to the library to link.
138 let mut v = OsString::from("-Wl,-force_load,");
c1a9b12d
SL
139 v.push(&archive::find_library(lib, search_path, &self.sess));
140 self.cmd.arg(&v);
141 }
142 }
143
144 fn link_whole_rlib(&mut self, lib: &Path) {
145 if self.sess.target.target.options.is_like_osx {
146 let mut v = OsString::from("-Wl,-force_load,");
147 v.push(lib);
62682a34 148 self.cmd.arg(&v);
c1a9b12d
SL
149 } else {
150 self.cmd.arg("-Wl,--whole-archive").arg(lib)
151 .arg("-Wl,--no-whole-archive");
62682a34
SL
152 }
153 }
154
a7813a04 155 fn gc_sections(&mut self, keep_metadata: bool) {
62682a34
SL
156 // The dead_strip option to the linker specifies that functions and data
157 // unreachable by the entry point will be removed. This is quite useful
158 // with Rust's compilation model of compiling libraries at a time into
159 // one object file. For example, this brings hello world from 1.7MB to
160 // 458K.
161 //
162 // Note that this is done for both executables and dynamic libraries. We
163 // won't get much benefit from dylibs because LLVM will have already
164 // stripped away as much as it could. This has not been seen to impact
165 // link times negatively.
166 //
167 // -dead_strip can't be part of the pre_link_args because it's also used
168 // for partial linking when using multiple codegen units (-r). So we
169 // insert it here.
170 if self.sess.target.target.options.is_like_osx {
171 self.cmd.arg("-Wl,-dead_strip");
7453a54e
SL
172 } else if self.sess.target.target.options.is_like_solaris {
173 self.cmd.arg("-Wl,-z");
174 self.cmd.arg("-Wl,ignore");
62682a34
SL
175
176 // If we're building a dylib, we don't use --gc-sections because LLVM
177 // has already done the best it can do, and we also don't want to
178 // eliminate the metadata. If we're building an executable, however,
179 // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
180 // reduction.
a7813a04 181 } else if !keep_metadata {
62682a34
SL
182 self.cmd.arg("-Wl,--gc-sections");
183 }
184 }
185
186 fn optimize(&mut self) {
187 if !self.sess.target.target.options.linker_is_gnu { return }
188
189 // GNU-style linkers support optimization with -O. GNU ld doesn't
190 // need a numeric argument, but other linkers do.
9cc50fc6
SL
191 if self.sess.opts.optimize == config::OptLevel::Default ||
192 self.sess.opts.optimize == config::OptLevel::Aggressive {
62682a34
SL
193 self.cmd.arg("-Wl,-O1");
194 }
195 }
196
c1a9b12d
SL
197 fn debuginfo(&mut self) {
198 // Don't do anything special here for GNU-style linkers.
199 }
200
62682a34 201 fn no_default_libraries(&mut self) {
b039eaaf 202 self.cmd.arg("-nodefaultlibs");
62682a34
SL
203 }
204
205 fn build_dylib(&mut self, out_filename: &Path) {
206 // On mac we need to tell the linker to let this library be rpathed
207 if self.sess.target.target.options.is_like_osx {
208 self.cmd.args(&["-dynamiclib", "-Wl,-dylib"]);
209
476ff2be
SL
210 // Note that the `osx_rpath_install_name` option here is a hack
211 // purely to support rustbuild right now, we should get a more
212 // principled solution at some point to force the compiler to pass
213 // the right `-Wl,-install_name` with an `@rpath` in it.
214 if self.sess.opts.cg.rpath ||
215 self.sess.opts.debugging_opts.osx_rpath_install_name {
62682a34
SL
216 let mut v = OsString::from("-Wl,-install_name,@rpath/");
217 v.push(out_filename.file_name().unwrap());
218 self.cmd.arg(&v);
219 }
220 } else {
221 self.cmd.arg("-shared");
222 }
223 }
224
225 fn whole_archives(&mut self) {
226 if !self.takes_hints() { return }
227 self.cmd.arg("-Wl,--whole-archive");
228 }
229
230 fn no_whole_archives(&mut self) {
231 if !self.takes_hints() { return }
232 self.cmd.arg("-Wl,--no-whole-archive");
233 }
234
235 fn hint_static(&mut self) {
236 if !self.takes_hints() { return }
237 self.cmd.arg("-Wl,-Bstatic");
238 }
239
240 fn hint_dynamic(&mut self) {
241 if !self.takes_hints() { return }
242 self.cmd.arg("-Wl,-Bdynamic");
243 }
e9174d1e 244
3157f602 245 fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
a7813a04
XL
246 // If we're compiling a dylib, then we let symbol visibility in object
247 // files to take care of whether they're exported or not.
248 //
249 // If we're compiling a cdylib, however, we manually create a list of
250 // exported symbols to ensure we don't expose any more. The object files
251 // have far more public symbols than we actually want to export, so we
252 // hide them all here.
9e0c209e 253 if crate_type == CrateType::CrateTypeDylib ||
c30ab7b3 254 crate_type == CrateType::CrateTypeProcMacro {
a7813a04
XL
255 return
256 }
257
9e0c209e 258 let mut arg = OsString::new();
a7813a04 259 let path = tmpdir.join("list");
9e0c209e 260
476ff2be
SL
261 debug!("EXPORTED SYMBOLS:");
262
263 if self.sess.target.target.options.is_like_osx {
264 // Write a plain, newline-separated list of symbols
9e0c209e
SL
265 let res = (|| -> io::Result<()> {
266 let mut f = BufWriter::new(File::create(&path)?);
9e0c209e 267 for sym in self.info.exports[&crate_type].iter() {
476ff2be
SL
268 debug!(" _{}", sym);
269 writeln!(f, "_{}", sym)?;
9e0c209e 270 }
9e0c209e
SL
271 Ok(())
272 })();
273 if let Err(e) = res {
476ff2be 274 self.sess.fatal(&format!("failed to write lib.def file: {}", e));
a7813a04 275 }
a7813a04 276 } else {
476ff2be 277 // Write an LD version script
9e0c209e
SL
278 let res = (|| -> io::Result<()> {
279 let mut f = BufWriter::new(File::create(&path)?);
476ff2be 280 writeln!(f, "{{\n global:")?;
9e0c209e 281 for sym in self.info.exports[&crate_type].iter() {
476ff2be
SL
282 debug!(" {};", sym);
283 writeln!(f, " {};", sym)?;
9e0c209e 284 }
476ff2be 285 writeln!(f, "\n local:\n *;\n}};")?;
9e0c209e
SL
286 Ok(())
287 })();
288 if let Err(e) = res {
476ff2be 289 self.sess.fatal(&format!("failed to write version script: {}", e));
9e0c209e 290 }
a7813a04 291 }
9e0c209e 292
476ff2be
SL
293 if self.sess.target.target.options.is_like_osx {
294 arg.push("-Wl,-exported_symbols_list,");
295 } else if self.sess.target.target.options.is_like_solaris {
296 arg.push("-Wl,-M,");
297 } else {
298 arg.push("-Wl,--version-script=");
299 }
300
301 arg.push(&path);
a7813a04 302 self.cmd.arg(arg);
e9174d1e 303 }
c30ab7b3
SL
304
305 fn subsystem(&mut self, subsystem: &str) {
306 self.cmd.arg(&format!("-Wl,--subsystem,{}", subsystem));
307 }
62682a34
SL
308}
309
310pub struct MsvcLinker<'a> {
3157f602
XL
311 cmd: &'a mut Command,
312 sess: &'a Session,
313 info: &'a LinkerInfo
62682a34
SL
314}
315
316impl<'a> Linker for MsvcLinker<'a> {
317 fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
318 fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
319 fn args(&mut self, args: &[String]) { self.cmd.args(args); }
7453a54e
SL
320
321 fn build_dylib(&mut self, out_filename: &Path) {
322 self.cmd.arg("/DLL");
323 let mut arg: OsString = "/IMPLIB:".into();
324 arg.push(out_filename.with_extension("dll.lib"));
325 self.cmd.arg(arg);
326 }
327
a7813a04 328 fn gc_sections(&mut self, _keep_metadata: bool) {
476ff2be
SL
329 // MSVC's ICF (Identical COMDAT Folding) link optimization is
330 // slow for Rust and thus we disable it by default when not in
331 // optimization build.
332 if self.sess.opts.optimize != config::OptLevel::No {
333 self.cmd.arg("/OPT:REF,ICF");
334 } else {
335 // It is necessary to specify NOICF here, because /OPT:REF
336 // implies ICF by default.
337 self.cmd.arg("/OPT:REF,NOICF");
338 }
a7813a04 339 }
62682a34
SL
340
341 fn link_dylib(&mut self, lib: &str) {
342 self.cmd.arg(&format!("{}.lib", lib));
343 }
c1a9b12d
SL
344
345 fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
346 // When producing a dll, the MSVC linker may not actually emit a
347 // `foo.lib` file if the dll doesn't actually export any symbols, so we
348 // check to see if the file is there and just omit linking to it if it's
349 // not present.
7453a54e 350 let name = format!("{}.dll.lib", lib);
c1a9b12d
SL
351 if fs::metadata(&path.join(&name)).is_ok() {
352 self.cmd.arg(name);
353 }
354 }
355
62682a34
SL
356 fn link_staticlib(&mut self, lib: &str) {
357 self.cmd.arg(&format!("{}.lib", lib));
358 }
359
360 fn position_independent_executable(&mut self) {
361 // noop
362 }
363
364 fn no_default_libraries(&mut self) {
365 // Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
366 // as there's been trouble in the past of linking the C++ standard
367 // library required by LLVM. This likely needs to happen one day, but
368 // in general Windows is also a more controlled environment than
369 // Unix, so it's not necessarily as critical that this be implemented.
370 //
371 // Note that there are also some licensing worries about statically
372 // linking some libraries which require a specific agreement, so it may
373 // not ever be possible for us to pass this flag.
374 }
375
376 fn include_path(&mut self, path: &Path) {
377 let mut arg = OsString::from("/LIBPATH:");
378 arg.push(path);
379 self.cmd.arg(&arg);
380 }
381
382 fn output_filename(&mut self, path: &Path) {
383 let mut arg = OsString::from("/OUT:");
384 arg.push(path);
385 self.cmd.arg(&arg);
386 }
387
388 fn framework_path(&mut self, _path: &Path) {
54a0048b 389 bug!("frameworks are not supported on windows")
62682a34
SL
390 }
391 fn link_framework(&mut self, _framework: &str) {
54a0048b 392 bug!("frameworks are not supported on windows")
62682a34
SL
393 }
394
395 fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
396 // not supported?
397 self.link_staticlib(lib);
398 }
c1a9b12d
SL
399 fn link_whole_rlib(&mut self, path: &Path) {
400 // not supported?
401 self.link_rlib(path);
402 }
62682a34
SL
403 fn optimize(&mut self) {
404 // Needs more investigation of `/OPT` arguments
405 }
c1a9b12d
SL
406
407 fn debuginfo(&mut self) {
b039eaaf
SL
408 // This will cause the Microsoft linker to generate a PDB file
409 // from the CodeView line tables in the object files.
410 self.cmd.arg("/DEBUG");
c1a9b12d
SL
411 }
412
62682a34
SL
413 fn whole_archives(&mut self) {
414 // hints not supported?
415 }
416 fn no_whole_archives(&mut self) {
417 // hints not supported?
418 }
419
420 // On windows static libraries are of the form `foo.lib` and dynamic
421 // libraries are not linked against directly, but rather through their
422 // import libraries also called `foo.lib`. As a result there's no
423 // possibility for a native library to appear both dynamically and
424 // statically in the same folder so we don't have to worry about hints like
425 // we do on Unix platforms.
426 fn hint_static(&mut self) {}
427 fn hint_dynamic(&mut self) {}
e9174d1e
SL
428
429 // Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
430 // export symbols from a dynamic library. When building a dynamic library,
431 // however, we're going to want some symbols exported, so this function
432 // generates a DEF file which lists all the symbols.
433 //
434 // The linker will read this `*.def` file and export all the symbols from
435 // the dynamic library. Note that this is not as simple as just exporting
436 // all the symbols in the current crate (as specified by `trans.reachable`)
437 // but rather we also need to possibly export the symbols of upstream
438 // crates. Upstream rlibs may be linked statically to this dynamic library,
439 // in which case they may continue to transitively be used and hence need
440 // their symbols exported.
a7813a04 441 fn export_symbols(&mut self,
a7813a04
XL
442 tmpdir: &Path,
443 crate_type: CrateType) {
e9174d1e
SL
444 let path = tmpdir.join("lib.def");
445 let res = (|| -> io::Result<()> {
54a0048b 446 let mut f = BufWriter::new(File::create(&path)?);
e9174d1e
SL
447
448 // Start off with the standard module name header and then go
449 // straight to exports.
54a0048b
SL
450 writeln!(f, "LIBRARY")?;
451 writeln!(f, "EXPORTS")?;
9e0c209e 452 for symbol in self.info.exports[&crate_type].iter() {
3157f602 453 writeln!(f, " {}", symbol)?;
e9174d1e 454 }
e9174d1e
SL
455 Ok(())
456 })();
457 if let Err(e) = res {
3157f602 458 self.sess.fatal(&format!("failed to write lib.def file: {}", e));
e9174d1e
SL
459 }
460 let mut arg = OsString::from("/DEF:");
461 arg.push(path);
462 self.cmd.arg(&arg);
463 }
c30ab7b3
SL
464
465 fn subsystem(&mut self, subsystem: &str) {
466 // Note that previous passes of the compiler validated this subsystem,
467 // so we just blindly pass it to the linker.
468 self.cmd.arg(&format!("/SUBSYSTEM:{}", subsystem));
469
470 // Windows has two subsystems we're interested in right now, the console
471 // and windows subsystems. These both implicitly have different entry
472 // points (starting symbols). The console entry point starts with
473 // `mainCRTStartup` and the windows entry point starts with
474 // `WinMainCRTStartup`. These entry points, defined in system libraries,
475 // will then later probe for either `main` or `WinMain`, respectively to
476 // start the application.
477 //
478 // In Rust we just always generate a `main` function so we want control
479 // to always start there, so we force the entry point on the windows
480 // subsystem to be `mainCRTStartup` to get everything booted up
481 // correctly.
482 //
483 // For more information see RFC #1665
484 if subsystem == "windows" {
485 self.cmd.arg("/ENTRY:mainCRTStartup");
486 }
487 }
62682a34 488}
a7813a04 489
3157f602 490fn exported_symbols(scx: &SharedCrateContext,
476ff2be 491 exported_symbols: &ExportedSymbols,
3157f602
XL
492 crate_type: CrateType)
493 -> Vec<String> {
476ff2be 494 let export_threshold = symbol_export::crate_export_threshold(crate_type);
a7813a04 495
476ff2be
SL
496 let mut symbols = Vec::new();
497 exported_symbols.for_each_exported_symbol(LOCAL_CRATE, export_threshold, |name, _| {
498 symbols.push(name.to_owned());
499 });
a7813a04 500
3157f602
XL
501 let formats = scx.sess().dependency_formats.borrow();
502 let deps = formats[&crate_type].iter();
476ff2be
SL
503
504 for (index, dep_format) in deps.enumerate() {
505 let cnum = CrateNum::new(index + 1);
506 // For each dependency that we are linking to statically ...
507 if *dep_format == Linkage::Static {
508 // ... we add its symbol list to our export list.
509 exported_symbols.for_each_exported_symbol(cnum, export_threshold, |name, _| {
510 symbols.push(name.to_owned());
511 })
a7813a04 512 }
476ff2be
SL
513 }
514
3157f602 515 symbols
a7813a04 516}