]>
Commit | Line | Data |
---|---|---|
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 | 11 | use std::collections::HashMap; |
62682a34 | 12 | use std::ffi::OsString; |
e9174d1e | 13 | use std::fs::{self, File}; |
e9174d1e | 14 | use std::io::prelude::*; |
9e0c209e | 15 | use std::io::{self, BufWriter}; |
62682a34 SL |
16 | use std::path::{Path, PathBuf}; |
17 | use std::process::Command; | |
18 | ||
3157f602 XL |
19 | use context::SharedCrateContext; |
20 | use monomorphize::Instance; | |
21 | ||
c1a9b12d | 22 | use back::archive; |
e9174d1e | 23 | use middle::dependency_format::Linkage; |
9e0c209e | 24 | use rustc::hir::def_id::CrateNum; |
62682a34 | 25 | use session::Session; |
a7813a04 | 26 | use session::config::CrateType; |
e9174d1e | 27 | use 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. | |
31 | pub struct LinkerInfo { | |
9e0c209e | 32 | exports: HashMap<CrateType, Vec<String>>, |
3157f602 XL |
33 | } |
34 | ||
35 | impl<'a, 'tcx> LinkerInfo { | |
36 | pub fn new(scx: &SharedCrateContext<'a, 'tcx>, | |
37 | reachable: &[String]) -> LinkerInfo { | |
38 | LinkerInfo { | |
9e0c209e SL |
39 | exports: scx.sess().crate_types.borrow().iter().map(|&c| { |
40 | (c, exported_symbols(scx, reachable, c)) | |
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. | |
71 | pub 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); |
62682a34 SL |
95 | } |
96 | ||
97 | pub struct GnuLinker<'a> { | |
3157f602 XL |
98 | cmd: &'a mut Command, |
99 | sess: &'a Session, | |
100 | info: &'a LinkerInfo | |
62682a34 SL |
101 | } |
102 | ||
103 | impl<'a> GnuLinker<'a> { | |
104 | fn takes_hints(&self) -> bool { | |
105 | !self.sess.target.target.options.is_like_osx | |
106 | } | |
107 | } | |
108 | ||
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); } | |
119 | ||
c1a9b12d SL |
120 | fn link_rust_dylib(&mut self, lib: &str, _path: &Path) { |
121 | self.cmd.arg("-l").arg(lib); | |
122 | } | |
123 | ||
62682a34 SL |
124 | fn link_framework(&mut self, framework: &str) { |
125 | self.cmd.arg("-framework").arg(framework); | |
126 | } | |
127 | ||
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") | |
132 | .arg("-l").arg(lib) | |
133 | .arg("-Wl,--no-whole-archive"); | |
134 | } else { | |
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,"); | |
c1a9b12d SL |
138 | v.push(&archive::find_library(lib, search_path, &self.sess)); |
139 | self.cmd.arg(&v); | |
140 | } | |
141 | } | |
142 | ||
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,"); | |
146 | v.push(lib); | |
62682a34 | 147 | self.cmd.arg(&v); |
c1a9b12d SL |
148 | } else { |
149 | self.cmd.arg("-Wl,--whole-archive").arg(lib) | |
150 | .arg("-Wl,--no-whole-archive"); | |
62682a34 SL |
151 | } |
152 | } | |
153 | ||
a7813a04 | 154 | fn gc_sections(&mut self, keep_metadata: bool) { |
62682a34 SL |
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 | |
159 | // 458K. | |
160 | // | |
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. | |
165 | // | |
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 | |
168 | // insert it here. | |
169 | if self.sess.target.target.options.is_like_osx { | |
170 | self.cmd.arg("-Wl,-dead_strip"); | |
7453a54e SL |
171 | } else if self.sess.target.target.options.is_like_solaris { |
172 | self.cmd.arg("-Wl,-z"); | |
173 | self.cmd.arg("-Wl,ignore"); | |
62682a34 SL |
174 | |
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% | |
179 | // reduction. | |
a7813a04 | 180 | } else if !keep_metadata { |
62682a34 SL |
181 | self.cmd.arg("-Wl,--gc-sections"); |
182 | } | |
183 | } | |
184 | ||
185 | fn optimize(&mut self) { | |
186 | if !self.sess.target.target.options.linker_is_gnu { return } | |
187 | ||
188 | // GNU-style linkers support optimization with -O. GNU ld doesn't | |
189 | // need a numeric argument, but other linkers do. | |
9cc50fc6 SL |
190 | if self.sess.opts.optimize == config::OptLevel::Default || |
191 | self.sess.opts.optimize == config::OptLevel::Aggressive { | |
62682a34 SL |
192 | self.cmd.arg("-Wl,-O1"); |
193 | } | |
194 | } | |
195 | ||
c1a9b12d SL |
196 | fn debuginfo(&mut self) { |
197 | // Don't do anything special here for GNU-style linkers. | |
198 | } | |
199 | ||
62682a34 | 200 | fn no_default_libraries(&mut self) { |
b039eaaf | 201 | self.cmd.arg("-nodefaultlibs"); |
62682a34 SL |
202 | } |
203 | ||
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"]); | |
208 | ||
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()); | |
212 | self.cmd.arg(&v); | |
213 | } | |
214 | } else { | |
215 | self.cmd.arg("-shared"); | |
216 | } | |
217 | } | |
218 | ||
219 | fn whole_archives(&mut self) { | |
220 | if !self.takes_hints() { return } | |
221 | self.cmd.arg("-Wl,--whole-archive"); | |
222 | } | |
223 | ||
224 | fn no_whole_archives(&mut self) { | |
225 | if !self.takes_hints() { return } | |
226 | self.cmd.arg("-Wl,--no-whole-archive"); | |
227 | } | |
228 | ||
229 | fn hint_static(&mut self) { | |
230 | if !self.takes_hints() { return } | |
231 | self.cmd.arg("-Wl,-Bstatic"); | |
232 | } | |
233 | ||
234 | fn hint_dynamic(&mut self) { | |
235 | if !self.takes_hints() { return } | |
236 | self.cmd.arg("-Wl,-Bdynamic"); | |
237 | } | |
e9174d1e | 238 | |
3157f602 | 239 | fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) { |
a7813a04 XL |
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. | |
242 | // | |
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. | |
9e0c209e SL |
247 | if crate_type == CrateType::CrateTypeDylib || |
248 | crate_type == CrateType::CrateTypeRustcMacro { | |
a7813a04 XL |
249 | return |
250 | } | |
251 | ||
9e0c209e | 252 | let mut arg = OsString::new(); |
a7813a04 | 253 | let path = tmpdir.join("list"); |
9e0c209e SL |
254 | |
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)?; | |
261 | } | |
262 | writeln!(f, "\n local:\n *;\n}};")?; | |
263 | Ok(()) | |
264 | })(); | |
265 | if let Err(e) = res { | |
266 | self.sess.fatal(&format!("failed to write version script: {}", e)); | |
a7813a04 | 267 | } |
9e0c209e SL |
268 | |
269 | arg.push("-Wl,-M,"); | |
270 | arg.push(&path); | |
a7813a04 | 271 | } else { |
9e0c209e SL |
272 | let prefix = if self.sess.target.target.options.is_like_osx { |
273 | "_" | |
274 | } else { | |
275 | "" | |
276 | }; | |
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)?; | |
281 | } | |
282 | Ok(()) | |
283 | })(); | |
284 | if let Err(e) = res { | |
285 | self.sess.fatal(&format!("failed to write lib.def file: {}", e)); | |
286 | } | |
287 | if self.sess.target.target.options.is_like_osx { | |
288 | arg.push("-Wl,-exported_symbols_list,"); | |
289 | } else { | |
290 | arg.push("-Wl,--retain-symbols-file="); | |
291 | } | |
292 | arg.push(&path); | |
a7813a04 | 293 | } |
9e0c209e | 294 | |
a7813a04 | 295 | self.cmd.arg(arg); |
e9174d1e | 296 | } |
62682a34 SL |
297 | } |
298 | ||
299 | pub struct MsvcLinker<'a> { | |
3157f602 XL |
300 | cmd: &'a mut Command, |
301 | sess: &'a Session, | |
302 | info: &'a LinkerInfo | |
62682a34 SL |
303 | } |
304 | ||
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); } | |
7453a54e SL |
309 | |
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")); | |
314 | self.cmd.arg(arg); | |
315 | } | |
316 | ||
a7813a04 XL |
317 | fn gc_sections(&mut self, _keep_metadata: bool) { |
318 | self.cmd.arg("/OPT:REF,ICF"); | |
319 | } | |
62682a34 SL |
320 | |
321 | fn link_dylib(&mut self, lib: &str) { | |
322 | self.cmd.arg(&format!("{}.lib", lib)); | |
323 | } | |
c1a9b12d SL |
324 | |
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 | |
329 | // not present. | |
7453a54e | 330 | let name = format!("{}.dll.lib", lib); |
c1a9b12d SL |
331 | if fs::metadata(&path.join(&name)).is_ok() { |
332 | self.cmd.arg(name); | |
333 | } | |
334 | } | |
335 | ||
62682a34 SL |
336 | fn link_staticlib(&mut self, lib: &str) { |
337 | self.cmd.arg(&format!("{}.lib", lib)); | |
338 | } | |
339 | ||
340 | fn position_independent_executable(&mut self) { | |
341 | // noop | |
342 | } | |
343 | ||
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. | |
350 | // | |
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. | |
354 | } | |
355 | ||
356 | fn include_path(&mut self, path: &Path) { | |
357 | let mut arg = OsString::from("/LIBPATH:"); | |
358 | arg.push(path); | |
359 | self.cmd.arg(&arg); | |
360 | } | |
361 | ||
362 | fn output_filename(&mut self, path: &Path) { | |
363 | let mut arg = OsString::from("/OUT:"); | |
364 | arg.push(path); | |
365 | self.cmd.arg(&arg); | |
366 | } | |
367 | ||
368 | fn framework_path(&mut self, _path: &Path) { | |
54a0048b | 369 | bug!("frameworks are not supported on windows") |
62682a34 SL |
370 | } |
371 | fn link_framework(&mut self, _framework: &str) { | |
54a0048b | 372 | bug!("frameworks are not supported on windows") |
62682a34 SL |
373 | } |
374 | ||
375 | fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) { | |
376 | // not supported? | |
377 | self.link_staticlib(lib); | |
378 | } | |
c1a9b12d SL |
379 | fn link_whole_rlib(&mut self, path: &Path) { |
380 | // not supported? | |
381 | self.link_rlib(path); | |
382 | } | |
62682a34 SL |
383 | fn optimize(&mut self) { |
384 | // Needs more investigation of `/OPT` arguments | |
385 | } | |
c1a9b12d SL |
386 | |
387 | fn debuginfo(&mut self) { | |
b039eaaf SL |
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"); | |
c1a9b12d SL |
391 | } |
392 | ||
62682a34 SL |
393 | fn whole_archives(&mut self) { |
394 | // hints not supported? | |
395 | } | |
396 | fn no_whole_archives(&mut self) { | |
397 | // hints not supported? | |
398 | } | |
399 | ||
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) {} | |
e9174d1e SL |
408 | |
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. | |
413 | // | |
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. | |
a7813a04 | 421 | fn export_symbols(&mut self, |
a7813a04 XL |
422 | tmpdir: &Path, |
423 | crate_type: CrateType) { | |
e9174d1e SL |
424 | let path = tmpdir.join("lib.def"); |
425 | let res = (|| -> io::Result<()> { | |
54a0048b | 426 | let mut f = BufWriter::new(File::create(&path)?); |
e9174d1e SL |
427 | |
428 | // Start off with the standard module name header and then go | |
429 | // straight to exports. | |
54a0048b SL |
430 | writeln!(f, "LIBRARY")?; |
431 | writeln!(f, "EXPORTS")?; | |
9e0c209e | 432 | for symbol in self.info.exports[&crate_type].iter() { |
3157f602 | 433 | writeln!(f, " {}", symbol)?; |
e9174d1e | 434 | } |
e9174d1e SL |
435 | Ok(()) |
436 | })(); | |
437 | if let Err(e) = res { | |
3157f602 | 438 | self.sess.fatal(&format!("failed to write lib.def file: {}", e)); |
e9174d1e SL |
439 | } |
440 | let mut arg = OsString::from("/DEF:"); | |
441 | arg.push(path); | |
442 | self.cmd.arg(&arg); | |
443 | } | |
62682a34 | 444 | } |
a7813a04 | 445 | |
3157f602 XL |
446 | fn exported_symbols(scx: &SharedCrateContext, |
447 | reachable: &[String], | |
448 | crate_type: CrateType) | |
449 | -> Vec<String> { | |
3157f602 XL |
450 | // See explanation in GnuLinker::export_symbols, for |
451 | // why we don't ever need dylib symbols on non-MSVC. | |
9e0c209e SL |
452 | if crate_type == CrateType::CrateTypeDylib || |
453 | crate_type == CrateType::CrateTypeRustcMacro { | |
3157f602 XL |
454 | if !scx.sess().target.target.options.is_like_msvc { |
455 | return vec![]; | |
456 | } | |
457 | } | |
458 | ||
459 | let mut symbols = reachable.to_vec(); | |
a7813a04 XL |
460 | |
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. | |
463 | // | |
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 { | |
468 | return symbols | |
469 | } | |
470 | ||
3157f602 XL |
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)| { | |
a7813a04 | 475 | if *f == Linkage::Static { |
9e0c209e | 476 | Some(CrateNum::new(i + 1)) |
a7813a04 XL |
477 | } else { |
478 | None | |
479 | } | |
480 | }).flat_map(|cnum| { | |
481 | cstore.reachable_ids(cnum) | |
3157f602 XL |
482 | }).map(|did| -> String { |
483 | Instance::mono(scx, did).symbol_name(scx) | |
a7813a04 | 484 | })); |
3157f602 | 485 | symbols |
a7813a04 | 486 | } |