]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/back/linker.rs
518a6c248407a995e38a6edc990b791c8fd18ad9
[rust.git] / src / librustc_trans / 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::ffi::OsString;
12 use std::path::{Path, PathBuf};
13 use std::process::Command;
14 use std::fs;
15
16 use rustc_back::archive;
17 use session::Session;
18 use session::config;
19
20 /// Linker abstraction used by back::link to build up the command to invoke a
21 /// linker.
22 ///
23 /// This trait is the total list of requirements needed by `back::link` and
24 /// represents the meaning of each option being passed down. This trait is then
25 /// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
26 /// MSVC linker (e.g. `link.exe`) is being used.
27 pub trait Linker {
28     fn link_dylib(&mut self, lib: &str);
29     fn link_rust_dylib(&mut self, lib: &str, path: &Path);
30     fn link_framework(&mut self, framework: &str);
31     fn link_staticlib(&mut self, lib: &str);
32     fn link_rlib(&mut self, lib: &Path);
33     fn link_whole_rlib(&mut self, lib: &Path);
34     fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]);
35     fn include_path(&mut self, path: &Path);
36     fn framework_path(&mut self, path: &Path);
37     fn output_filename(&mut self, path: &Path);
38     fn add_object(&mut self, path: &Path);
39     fn gc_sections(&mut self, is_dylib: bool);
40     fn position_independent_executable(&mut self);
41     fn optimize(&mut self);
42     fn no_default_libraries(&mut self);
43     fn build_dylib(&mut self, out_filename: &Path);
44     fn args(&mut self, args: &[String]);
45     fn hint_static(&mut self);
46     fn hint_dynamic(&mut self);
47     fn whole_archives(&mut self);
48     fn no_whole_archives(&mut self);
49 }
50
51 pub struct GnuLinker<'a> {
52     pub cmd: &'a mut Command,
53     pub sess: &'a Session,
54 }
55
56 impl<'a> GnuLinker<'a> {
57     fn takes_hints(&self) -> bool {
58         !self.sess.target.target.options.is_like_osx
59     }
60 }
61
62 impl<'a> Linker for GnuLinker<'a> {
63     fn link_dylib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
64     fn link_staticlib(&mut self, lib: &str) { self.cmd.arg("-l").arg(lib); }
65     fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
66     fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); }
67     fn framework_path(&mut self, path: &Path) { self.cmd.arg("-F").arg(path); }
68     fn output_filename(&mut self, path: &Path) { self.cmd.arg("-o").arg(path); }
69     fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
70     fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
71     fn args(&mut self, args: &[String]) { self.cmd.args(args); }
72
73     fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
74         self.cmd.arg("-l").arg(lib);
75     }
76
77     fn link_framework(&mut self, framework: &str) {
78         self.cmd.arg("-framework").arg(framework);
79     }
80
81     fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
82         let target = &self.sess.target.target;
83         if !target.options.is_like_osx {
84             self.cmd.arg("-Wl,--whole-archive")
85                     .arg("-l").arg(lib)
86                     .arg("-Wl,--no-whole-archive");
87         } else {
88             // -force_load is the OSX equivalent of --whole-archive, but it
89             // involves passing the full path to the library to link.
90             let mut v = OsString::from("-Wl,-force_load,");
91             v.push(&archive::find_library(lib,
92                                           &target.options.staticlib_prefix,
93                                           &target.options.staticlib_suffix,
94                                           search_path,
95                                           &self.sess.diagnostic().handler));
96             self.cmd.arg(&v);
97         }
98     }
99
100     fn link_whole_rlib(&mut self, lib: &Path) {
101         if self.sess.target.target.options.is_like_osx {
102             let mut v = OsString::from("-Wl,-force_load,");
103             v.push(lib);
104             self.cmd.arg(&v);
105         } else {
106             self.cmd.arg("-Wl,--whole-archive").arg(lib)
107                     .arg("-Wl,--no-whole-archive");
108         }
109     }
110
111     fn gc_sections(&mut self, is_dylib: bool) {
112         // The dead_strip option to the linker specifies that functions and data
113         // unreachable by the entry point will be removed. This is quite useful
114         // with Rust's compilation model of compiling libraries at a time into
115         // one object file. For example, this brings hello world from 1.7MB to
116         // 458K.
117         //
118         // Note that this is done for both executables and dynamic libraries. We
119         // won't get much benefit from dylibs because LLVM will have already
120         // stripped away as much as it could. This has not been seen to impact
121         // link times negatively.
122         //
123         // -dead_strip can't be part of the pre_link_args because it's also used
124         // for partial linking when using multiple codegen units (-r).  So we
125         // insert it here.
126         if self.sess.target.target.options.is_like_osx {
127             self.cmd.arg("-Wl,-dead_strip");
128
129         // If we're building a dylib, we don't use --gc-sections because LLVM
130         // has already done the best it can do, and we also don't want to
131         // eliminate the metadata. If we're building an executable, however,
132         // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
133         // reduction.
134         } else if !is_dylib {
135             self.cmd.arg("-Wl,--gc-sections");
136         }
137     }
138
139     fn optimize(&mut self) {
140         if !self.sess.target.target.options.linker_is_gnu { return }
141
142         // GNU-style linkers support optimization with -O. GNU ld doesn't
143         // need a numeric argument, but other linkers do.
144         if self.sess.opts.optimize == config::Default ||
145            self.sess.opts.optimize == config::Aggressive {
146             self.cmd.arg("-Wl,-O1");
147         }
148     }
149
150     fn no_default_libraries(&mut self) {
151         // Unfortunately right now passing -nodefaultlibs to gcc on windows
152         // doesn't work so hot (in terms of native dependencies). This if
153         // statement should hopefully be removed one day though!
154         if !self.sess.target.target.options.is_like_windows {
155             self.cmd.arg("-nodefaultlibs");
156         }
157     }
158
159     fn build_dylib(&mut self, out_filename: &Path) {
160         // On mac we need to tell the linker to let this library be rpathed
161         if self.sess.target.target.options.is_like_osx {
162             self.cmd.args(&["-dynamiclib", "-Wl,-dylib"]);
163
164             if self.sess.opts.cg.rpath {
165                 let mut v = OsString::from("-Wl,-install_name,@rpath/");
166                 v.push(out_filename.file_name().unwrap());
167                 self.cmd.arg(&v);
168             }
169         } else {
170             self.cmd.arg("-shared");
171         }
172     }
173
174     fn whole_archives(&mut self) {
175         if !self.takes_hints() { return }
176         self.cmd.arg("-Wl,--whole-archive");
177     }
178
179     fn no_whole_archives(&mut self) {
180         if !self.takes_hints() { return }
181         self.cmd.arg("-Wl,--no-whole-archive");
182     }
183
184     fn hint_static(&mut self) {
185         if !self.takes_hints() { return }
186         self.cmd.arg("-Wl,-Bstatic");
187     }
188
189     fn hint_dynamic(&mut self) {
190         if !self.takes_hints() { return }
191         self.cmd.arg("-Wl,-Bdynamic");
192     }
193 }
194
195 pub struct MsvcLinker<'a> {
196     pub cmd: &'a mut Command,
197     pub sess: &'a Session,
198 }
199
200 impl<'a> Linker for MsvcLinker<'a> {
201     fn link_rlib(&mut self, lib: &Path) { self.cmd.arg(lib); }
202     fn add_object(&mut self, path: &Path) { self.cmd.arg(path); }
203     fn args(&mut self, args: &[String]) { self.cmd.args(args); }
204     fn build_dylib(&mut self, _out_filename: &Path) { self.cmd.arg("/DLL"); }
205     fn gc_sections(&mut self, _is_dylib: bool) { self.cmd.arg("/OPT:REF,ICF"); }
206
207     fn link_dylib(&mut self, lib: &str) {
208         self.cmd.arg(&format!("{}.lib", lib));
209     }
210
211     fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
212         // When producing a dll, the MSVC linker may not actually emit a
213         // `foo.lib` file if the dll doesn't actually export any symbols, so we
214         // check to see if the file is there and just omit linking to it if it's
215         // not present.
216         let name = format!("{}.lib", lib);
217         if fs::metadata(&path.join(&name)).is_ok() {
218             self.cmd.arg(name);
219         }
220     }
221
222     fn link_staticlib(&mut self, lib: &str) {
223         self.cmd.arg(&format!("{}.lib", lib));
224     }
225
226     fn position_independent_executable(&mut self) {
227         // noop
228     }
229
230     fn no_default_libraries(&mut self) {
231         // Currently we don't pass the /NODEFAULTLIB flag to the linker on MSVC
232         // as there's been trouble in the past of linking the C++ standard
233         // library required by LLVM. This likely needs to happen one day, but
234         // in general Windows is also a more controlled environment than
235         // Unix, so it's not necessarily as critical that this be implemented.
236         //
237         // Note that there are also some licensing worries about statically
238         // linking some libraries which require a specific agreement, so it may
239         // not ever be possible for us to pass this flag.
240     }
241
242     fn include_path(&mut self, path: &Path) {
243         let mut arg = OsString::from("/LIBPATH:");
244         arg.push(path);
245         self.cmd.arg(&arg);
246     }
247
248     fn output_filename(&mut self, path: &Path) {
249         let mut arg = OsString::from("/OUT:");
250         arg.push(path);
251         self.cmd.arg(&arg);
252     }
253
254     fn framework_path(&mut self, _path: &Path) {
255         panic!("frameworks are not supported on windows")
256     }
257     fn link_framework(&mut self, _framework: &str) {
258         panic!("frameworks are not supported on windows")
259     }
260
261     fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
262         // not supported?
263         self.link_staticlib(lib);
264     }
265     fn link_whole_rlib(&mut self, path: &Path) {
266         // not supported?
267         self.link_rlib(path);
268     }
269     fn optimize(&mut self) {
270         // Needs more investigation of `/OPT` arguments
271     }
272     fn whole_archives(&mut self) {
273         // hints not supported?
274     }
275     fn no_whole_archives(&mut self) {
276         // hints not supported?
277     }
278
279     // On windows static libraries are of the form `foo.lib` and dynamic
280     // libraries are not linked against directly, but rather through their
281     // import libraries also called `foo.lib`. As a result there's no
282     // possibility for a native library to appear both dynamically and
283     // statically in the same folder so we don't have to worry about hints like
284     // we do on Unix platforms.
285     fn hint_static(&mut self) {}
286     fn hint_dynamic(&mut self) {}
287 }