]>
Commit | Line | Data |
---|---|---|
dfeec247 XL |
1 | use super::command::Command; |
2 | use super::symbol_export; | |
2b03887a | 3 | use crate::errors; |
f035d41b | 4 | use rustc_span::symbol::sym; |
a1dfa0c6 | 5 | |
cc61c64b | 6 | use std::ffi::{OsStr, OsString}; |
e9174d1e | 7 | use std::fs::{self, File}; |
e9174d1e | 8 | use std::io::prelude::*; |
9e0c209e | 9 | use std::io::{self, BufWriter}; |
62682a34 | 10 | use std::path::{Path, PathBuf}; |
136023e0 | 11 | use std::{env, mem, str}; |
3157f602 | 12 | |
dfeec247 | 13 | use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; |
f2b60f7d | 14 | use rustc_metadata::find_native_static_library; |
ba9703b0 | 15 | use rustc_middle::middle::dependency_format::Linkage; |
04454e1e | 16 | use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportKind}; |
ba9703b0 | 17 | use rustc_middle::ty::TyCtxt; |
f9f354fc | 18 | use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip}; |
ba9703b0 | 19 | use rustc_session::Session; |
2b03887a | 20 | use rustc_target::spec::{Cc, LinkOutputKind, LinkerFlavor, Lld}; |
f9f354fc | 21 | |
136023e0 XL |
22 | use cc::windows_registry; |
23 | ||
f9f354fc XL |
24 | /// Disables non-English messages from localized linkers. |
25 | /// Such messages may cause issues with text encoding on Windows (#35785) | |
26 | /// and prevent inspection of linker output in case of errors, which we occasionally do. | |
27 | /// This should be acceptable because other messages from rustc are in English anyway, | |
28 | /// and may also be desirable to improve searchability of the linker diagnostics. | |
29 | pub fn disable_localization(linker: &mut Command) { | |
30 | // No harm in setting both env vars simultaneously. | |
31 | // Unix-style linkers. | |
32 | linker.env("LC_ALL", "C"); | |
33 | // MSVC's `link.exe`. | |
34 | linker.env("VSLANG", "1033"); | |
35 | } | |
3157f602 | 36 | |
487cf647 FG |
37 | /// The third parameter is for env vars, used on windows to set up the |
38 | /// path for MSVC to find its DLLs, and gcc to find its bundled | |
39 | /// toolchain | |
136023e0 XL |
40 | pub fn get_linker<'a>( |
41 | sess: &'a Session, | |
42 | linker: &Path, | |
43 | flavor: LinkerFlavor, | |
44 | self_contained: bool, | |
45 | target_cpu: &'a str, | |
46 | ) -> Box<dyn Linker + 'a> { | |
47 | let msvc_tool = windows_registry::find_tool(&sess.opts.target_triple.triple(), "link.exe"); | |
48 | ||
49 | // If our linker looks like a batch script on Windows then to execute this | |
50 | // we'll need to spawn `cmd` explicitly. This is primarily done to handle | |
51 | // emscripten where the linker is `emcc.bat` and needs to be spawned as | |
52 | // `cmd /c emcc.bat ...`. | |
53 | // | |
54 | // This worked historically but is needed manually since #42436 (regression | |
55 | // was tagged as #42791) and some more info can be found on #44443 for | |
56 | // emscripten itself. | |
57 | let mut cmd = match linker.to_str() { | |
58 | Some(linker) if cfg!(windows) && linker.ends_with(".bat") => Command::bat_script(linker), | |
59 | _ => match flavor { | |
2b03887a FG |
60 | LinkerFlavor::Gnu(Cc::No, Lld::Yes) |
61 | | LinkerFlavor::Darwin(Cc::No, Lld::Yes) | |
62 | | LinkerFlavor::WasmLld(Cc::No) | |
63 | | LinkerFlavor::Msvc(Lld::Yes) => Command::lld(linker, flavor.lld_flavor()), | |
64 | LinkerFlavor::Msvc(Lld::No) | |
65 | if sess.opts.cg.linker.is_none() && sess.target.linker.is_none() => | |
66 | { | |
136023e0 XL |
67 | Command::new(msvc_tool.as_ref().map_or(linker, |t| t.path())) |
68 | } | |
69 | _ => Command::new(linker), | |
70 | }, | |
71 | }; | |
72 | ||
73 | // UWP apps have API restrictions enforced during Store submissions. | |
74 | // To comply with the Windows App Certification Kit, | |
75 | // MSVC needs to link with the Store versions of the runtime libraries (vcruntime, msvcrt, etc). | |
76 | let t = &sess.target; | |
2b03887a | 77 | if matches!(flavor, LinkerFlavor::Msvc(..)) && t.vendor == "uwp" { |
136023e0 XL |
78 | if let Some(ref tool) = msvc_tool { |
79 | let original_path = tool.path(); | |
80 | if let Some(ref root_lib_path) = original_path.ancestors().nth(4) { | |
5e7ed085 | 81 | let arch = match t.arch.as_ref() { |
136023e0 XL |
82 | "x86_64" => Some("x64"), |
83 | "x86" => Some("x86"), | |
84 | "aarch64" => Some("arm64"), | |
85 | "arm" => Some("arm"), | |
86 | _ => None, | |
87 | }; | |
88 | if let Some(ref a) = arch { | |
89 | // FIXME: Move this to `fn linker_with_args`. | |
90 | let mut arg = OsString::from("/LIBPATH:"); | |
91 | arg.push(format!("{}\\lib\\{}\\store", root_lib_path.display(), a)); | |
92 | cmd.arg(&arg); | |
93 | } else { | |
94 | warn!("arch is not supported"); | |
95 | } | |
96 | } else { | |
97 | warn!("MSVC root path lib location not found"); | |
98 | } | |
99 | } else { | |
100 | warn!("link.exe not found"); | |
3157f602 XL |
101 | } |
102 | } | |
103 | ||
136023e0 XL |
104 | // The compiler's sysroot often has some bundled tools, so add it to the |
105 | // PATH for the child. | |
c295e0f8 | 106 | let mut new_path = sess.get_tools_search_paths(self_contained); |
136023e0 XL |
107 | let mut msvc_changed_path = false; |
108 | if sess.target.is_like_msvc { | |
109 | if let Some(ref tool) = msvc_tool { | |
110 | cmd.args(tool.args()); | |
9c376795 | 111 | for (k, v) in tool.env() { |
136023e0 XL |
112 | if k == "PATH" { |
113 | new_path.extend(env::split_paths(v)); | |
114 | msvc_changed_path = true; | |
115 | } else { | |
116 | cmd.env(k, v); | |
117 | } | |
17df50a5 | 118 | } |
136023e0 XL |
119 | } |
120 | } | |
dfeec247 | 121 | |
136023e0 XL |
122 | if !msvc_changed_path { |
123 | if let Some(path) = env::var_os("PATH") { | |
124 | new_path.extend(env::split_paths(&path)); | |
125 | } | |
126 | } | |
127 | cmd.env("PATH", env::join_paths(new_path).unwrap()); | |
0531ce1d | 128 | |
136023e0 XL |
129 | // FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction |
130 | // to the linker args construction. | |
131 | assert!(cmd.get_args().is_empty() || sess.target.vendor == "uwp"); | |
136023e0 | 132 | match flavor { |
2b03887a | 133 | LinkerFlavor::Unix(Cc::No) if sess.target.os == "l4re" => { |
f2b60f7d FG |
134 | Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker> |
135 | } | |
2b03887a FG |
136 | LinkerFlavor::WasmLld(Cc::No) => Box::new(WasmLd::new(cmd, sess)) as Box<dyn Linker>, |
137 | LinkerFlavor::Gnu(cc, _) | |
138 | | LinkerFlavor::Darwin(cc, _) | |
139 | | LinkerFlavor::WasmLld(cc) | |
140 | | LinkerFlavor::Unix(cc) => Box::new(GccLinker { | |
141 | cmd, | |
142 | sess, | |
143 | target_cpu, | |
144 | hinted_static: false, | |
145 | is_ld: cc == Cc::No, | |
146 | is_gnu: flavor.is_gnu(), | |
147 | }) as Box<dyn Linker>, | |
148 | LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>, | |
f2b60f7d FG |
149 | LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>, |
150 | LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>, | |
151 | LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>, | |
3157f602 XL |
152 | } |
153 | } | |
62682a34 | 154 | |
9fa01778 | 155 | /// Linker abstraction used by `back::link` to build up the command to invoke a |
62682a34 SL |
156 | /// linker. |
157 | /// | |
158 | /// This trait is the total list of requirements needed by `back::link` and | |
159 | /// represents the meaning of each option being passed down. This trait is then | |
160 | /// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an | |
0731742a | 161 | /// MSVC linker (e.g., `link.exe`) is being used. |
62682a34 | 162 | pub trait Linker { |
ba9703b0 | 163 | fn cmd(&mut self) -> &mut Command; |
f9f354fc | 164 | fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path); |
064997fb FG |
165 | fn link_dylib(&mut self, lib: &str, verbatim: bool, as_needed: bool); |
166 | fn link_rust_dylib(&mut self, lib: &str, path: &Path); | |
167 | fn link_framework(&mut self, framework: &str, as_needed: bool); | |
168 | fn link_staticlib(&mut self, lib: &str, verbatim: bool); | |
62682a34 | 169 | fn link_rlib(&mut self, lib: &Path); |
c1a9b12d | 170 | fn link_whole_rlib(&mut self, lib: &Path); |
064997fb | 171 | fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, search_path: &[PathBuf]); |
62682a34 SL |
172 | fn include_path(&mut self, path: &Path); |
173 | fn framework_path(&mut self, path: &Path); | |
174 | fn output_filename(&mut self, path: &Path); | |
175 | fn add_object(&mut self, path: &Path); | |
a7813a04 | 176 | fn gc_sections(&mut self, keep_metadata: bool); |
17df50a5 | 177 | fn no_gc_sections(&mut self); |
3b2f2976 | 178 | fn full_relro(&mut self); |
0531ce1d XL |
179 | fn partial_relro(&mut self); |
180 | fn no_relro(&mut self); | |
62682a34 | 181 | fn optimize(&mut self); |
0531ce1d | 182 | fn pgo_gen(&mut self); |
74b04a01 | 183 | fn control_flow_guard(&mut self); |
923072b8 | 184 | fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]); |
f9f354fc | 185 | fn no_crt_objects(&mut self); |
62682a34 | 186 | fn no_default_libraries(&mut self); |
136023e0 | 187 | fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]); |
c30ab7b3 | 188 | fn subsystem(&mut self, subsystem: &str); |
9fa01778 | 189 | fn linker_plugin_lto(&mut self); |
f035d41b | 190 | fn add_eh_frame_header(&mut self) {} |
cdc7bbd5 XL |
191 | fn add_no_exec(&mut self) {} |
192 | fn add_as_needed(&mut self) {} | |
17df50a5 | 193 | fn reset_per_library_state(&mut self) {} |
ba9703b0 XL |
194 | } |
195 | ||
196 | impl dyn Linker + '_ { | |
197 | pub fn arg(&mut self, arg: impl AsRef<OsStr>) { | |
198 | self.cmd().arg(arg); | |
199 | } | |
200 | ||
201 | pub fn args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) { | |
202 | self.cmd().args(args); | |
203 | } | |
204 | ||
205 | pub fn take_cmd(&mut self) -> Command { | |
206 | mem::replace(self.cmd(), Command::new("")) | |
207 | } | |
62682a34 SL |
208 | } |
209 | ||
cc61c64b XL |
210 | pub struct GccLinker<'a> { |
211 | cmd: Command, | |
3157f602 | 212 | sess: &'a Session, |
136023e0 | 213 | target_cpu: &'a str, |
cc61c64b XL |
214 | hinted_static: bool, // Keeps track of the current hinting mode. |
215 | // Link as ld | |
216 | is_ld: bool, | |
2b03887a | 217 | is_gnu: bool, |
62682a34 SL |
218 | } |
219 | ||
cc61c64b | 220 | impl<'a> GccLinker<'a> { |
3c0e092e | 221 | /// Passes an argument directly to the linker. |
cc61c64b | 222 | /// |
3c0e092e XL |
223 | /// When the linker is not ld-like such as when using a compiler as a linker, the argument is |
224 | /// prepended by `-Wl,`. | |
225 | fn linker_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self { | |
226 | self.linker_args(&[arg]); | |
227 | self | |
228 | } | |
229 | ||
230 | /// Passes a series of arguments directly to the linker. | |
231 | /// | |
232 | /// When the linker is ld-like, the arguments are simply appended to the command. When the | |
233 | /// linker is not ld-like such as when using a compiler as a linker, the arguments are joined by | |
234 | /// commas to form an argument that is then prepended with `-Wl`. In this situation, only a | |
235 | /// single argument is appended to the command to ensure that the order of the arguments is | |
236 | /// preserved by the compiler. | |
237 | fn linker_args(&mut self, args: &[impl AsRef<OsStr>]) -> &mut Self { | |
238 | if self.is_ld { | |
239 | args.into_iter().for_each(|a| { | |
240 | self.cmd.arg(a); | |
241 | }); | |
cc61c64b | 242 | } else { |
3c0e092e XL |
243 | if !args.is_empty() { |
244 | let mut s = OsString::from("-Wl"); | |
245 | for a in args { | |
246 | s.push(","); | |
247 | s.push(a); | |
248 | } | |
249 | self.cmd.arg(s); | |
250 | } | |
cc61c64b XL |
251 | } |
252 | self | |
253 | } | |
254 | ||
62682a34 | 255 | fn takes_hints(&self) -> bool { |
532ac7d7 XL |
256 | // Really this function only returns true if the underlying linker |
257 | // configured for a compiler is binutils `ld.bfd` and `ld.gold`. We | |
258 | // don't really have a foolproof way to detect that, so rule out some | |
259 | // platforms where currently this is guaranteed to *not* be the case: | |
260 | // | |
261 | // * On OSX they have their own linker, not binutils' | |
262 | // * For WebAssembly the only functional linker is LLD, which doesn't | |
263 | // support hint flags | |
cdc7bbd5 | 264 | !self.sess.target.is_like_osx && !self.sess.target.is_like_wasm |
62682a34 | 265 | } |
cc61c64b XL |
266 | |
267 | // Some platforms take hints about whether a library is static or dynamic. | |
268 | // For those that support this, we ensure we pass the option if the library | |
269 | // was flagged "static" (most defaults are dynamic) to ensure that if | |
270 | // libfoo.a and libfoo.so both exist that the right one is chosen. | |
271 | fn hint_static(&mut self) { | |
dfeec247 XL |
272 | if !self.takes_hints() { |
273 | return; | |
274 | } | |
cc61c64b XL |
275 | if !self.hinted_static { |
276 | self.linker_arg("-Bstatic"); | |
277 | self.hinted_static = true; | |
278 | } | |
279 | } | |
280 | ||
281 | fn hint_dynamic(&mut self) { | |
dfeec247 XL |
282 | if !self.takes_hints() { |
283 | return; | |
284 | } | |
cc61c64b XL |
285 | if self.hinted_static { |
286 | self.linker_arg("-Bdynamic"); | |
287 | self.hinted_static = false; | |
288 | } | |
289 | } | |
8faf50e0 | 290 | |
9fa01778 | 291 | fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) { |
8faf50e0 XL |
292 | if let Some(plugin_path) = plugin_path { |
293 | let mut arg = OsString::from("-plugin="); | |
294 | arg.push(plugin_path); | |
295 | self.linker_arg(&arg); | |
296 | } | |
297 | ||
298 | let opt_level = match self.sess.opts.optimize { | |
299 | config::OptLevel::No => "O0", | |
300 | config::OptLevel::Less => "O1", | |
29967ef6 | 301 | config::OptLevel::Default | config::OptLevel::Size | config::OptLevel::SizeMin => "O2", |
8faf50e0 | 302 | config::OptLevel::Aggressive => "O3", |
8faf50e0 XL |
303 | }; |
304 | ||
064997fb | 305 | if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use { |
c295e0f8 XL |
306 | self.linker_arg(&format!("-plugin-opt=sample-profile={}", path.display())); |
307 | }; | |
3c0e092e XL |
308 | self.linker_args(&[ |
309 | &format!("-plugin-opt={}", opt_level), | |
310 | &format!("-plugin-opt=mcpu={}", self.target_cpu), | |
311 | ]); | |
8faf50e0 | 312 | } |
f9f354fc XL |
313 | |
314 | fn build_dylib(&mut self, out_filename: &Path) { | |
315 | // On mac we need to tell the linker to let this library be rpathed | |
29967ef6 | 316 | if self.sess.target.is_like_osx { |
3c0e092e XL |
317 | if !self.is_ld { |
318 | self.cmd.arg("-dynamiclib"); | |
319 | } | |
320 | ||
f9f354fc XL |
321 | self.linker_arg("-dylib"); |
322 | ||
323 | // Note that the `osx_rpath_install_name` option here is a hack | |
324 | // purely to support rustbuild right now, we should get a more | |
325 | // principled solution at some point to force the compiler to pass | |
326 | // the right `-Wl,-install_name` with an `@rpath` in it. | |
064997fb | 327 | if self.sess.opts.cg.rpath || self.sess.opts.unstable_opts.osx_rpath_install_name { |
3c0e092e XL |
328 | let mut rpath = OsString::from("@rpath/"); |
329 | rpath.push(out_filename.file_name().unwrap()); | |
330 | self.linker_args(&[OsString::from("-install_name"), rpath]); | |
f9f354fc XL |
331 | } |
332 | } else { | |
333 | self.cmd.arg("-shared"); | |
29967ef6 | 334 | if self.sess.target.is_like_windows { |
f9f354fc XL |
335 | // The output filename already contains `dll_suffix` so |
336 | // the resulting import library will have a name in the | |
337 | // form of libfoo.dll.a | |
338 | let implib_name = | |
339 | out_filename.file_name().and_then(|file| file.to_str()).map(|file| { | |
340 | format!( | |
341 | "{}{}{}", | |
29967ef6 | 342 | self.sess.target.staticlib_prefix, |
f9f354fc | 343 | file, |
29967ef6 | 344 | self.sess.target.staticlib_suffix |
f9f354fc XL |
345 | ) |
346 | }); | |
347 | if let Some(implib_name) = implib_name { | |
348 | let implib = out_filename.parent().map(|dir| dir.join(&implib_name)); | |
349 | if let Some(implib) = implib { | |
3dfed10e | 350 | self.linker_arg(&format!("--out-implib={}", (*implib).to_str().unwrap())); |
f9f354fc XL |
351 | } |
352 | } | |
353 | } | |
354 | } | |
355 | } | |
62682a34 SL |
356 | } |
357 | ||
cc61c64b | 358 | impl<'a> Linker for GccLinker<'a> { |
ba9703b0 XL |
359 | fn cmd(&mut self) -> &mut Command { |
360 | &mut self.cmd | |
361 | } | |
f9f354fc XL |
362 | |
363 | fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) { | |
364 | match output_kind { | |
365 | LinkOutputKind::DynamicNoPicExe => { | |
2b03887a | 366 | if !self.is_ld && self.is_gnu { |
f9f354fc XL |
367 | self.cmd.arg("-no-pie"); |
368 | } | |
369 | } | |
370 | LinkOutputKind::DynamicPicExe => { | |
17df50a5 XL |
371 | // noop on windows w/ gcc & ld, error w/ lld |
372 | if !self.sess.target.is_like_windows { | |
373 | // `-pie` works for both gcc wrapper and ld. | |
374 | self.cmd.arg("-pie"); | |
375 | } | |
f9f354fc XL |
376 | } |
377 | LinkOutputKind::StaticNoPicExe => { | |
378 | // `-static` works for both gcc wrapper and ld. | |
379 | self.cmd.arg("-static"); | |
2b03887a | 380 | if !self.is_ld && self.is_gnu { |
f9f354fc XL |
381 | self.cmd.arg("-no-pie"); |
382 | } | |
383 | } | |
384 | LinkOutputKind::StaticPicExe => { | |
385 | if !self.is_ld { | |
386 | // Note that combination `-static -pie` doesn't work as expected | |
387 | // for the gcc wrapper, `-static` in that case suppresses `-pie`. | |
388 | self.cmd.arg("-static-pie"); | |
389 | } else { | |
390 | // `--no-dynamic-linker` and `-z text` are not strictly necessary for producing | |
391 | // a static pie, but currently passed because gcc and clang pass them. | |
392 | // The former suppresses the `INTERP` ELF header specifying dynamic linker, | |
393 | // which is otherwise implicitly injected by ld (but not lld). | |
394 | // The latter doesn't change anything, only ensures that everything is pic. | |
395 | self.cmd.args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]); | |
396 | } | |
397 | } | |
398 | LinkOutputKind::DynamicDylib => self.build_dylib(out_filename), | |
399 | LinkOutputKind::StaticDylib => { | |
400 | self.cmd.arg("-static"); | |
401 | self.build_dylib(out_filename); | |
402 | } | |
5869c6ff | 403 | LinkOutputKind::WasiReactorExe => { |
3c0e092e | 404 | self.linker_args(&["--entry", "_initialize"]); |
5869c6ff | 405 | } |
f9f354fc | 406 | } |
f035d41b XL |
407 | // VxWorks compiler driver introduced `--static-crt` flag specifically for rustc, |
408 | // it switches linking for libc and similar system libraries to static without using | |
409 | // any `#[link]` attributes in the `libc` crate, see #72782 for details. | |
410 | // FIXME: Switch to using `#[link]` attributes in the `libc` crate | |
411 | // similarly to other targets. | |
29967ef6 | 412 | if self.sess.target.os == "vxworks" |
f035d41b XL |
413 | && matches!( |
414 | output_kind, | |
415 | LinkOutputKind::StaticNoPicExe | |
416 | | LinkOutputKind::StaticPicExe | |
417 | | LinkOutputKind::StaticDylib | |
418 | ) | |
419 | { | |
420 | self.cmd.arg("--static-crt"); | |
421 | } | |
f9f354fc XL |
422 | } |
423 | ||
064997fb FG |
424 | fn link_dylib(&mut self, lib: &str, verbatim: bool, as_needed: bool) { |
425 | if self.sess.target.os == "illumos" && lib == "c" { | |
17df50a5 XL |
426 | // libc will be added via late_link_args on illumos so that it will |
427 | // appear last in the library search order. | |
428 | // FIXME: This should be replaced by a more complete and generic | |
429 | // mechanism for controlling the order of library arguments passed | |
430 | // to the linker. | |
431 | return; | |
432 | } | |
433 | if !as_needed { | |
434 | if self.sess.target.is_like_osx { | |
435 | // FIXME(81490): ld64 doesn't support these flags but macOS 11 | |
436 | // has -needed-l{} / -needed_library {} | |
437 | // but we have no way to detect that here. | |
2b03887a FG |
438 | self.sess.emit_warning(errors::Ld64UnimplementedModifier); |
439 | } else if self.is_gnu && !self.sess.target.is_like_windows { | |
17df50a5 XL |
440 | self.linker_arg("--no-as-needed"); |
441 | } else { | |
2b03887a | 442 | self.sess.emit_warning(errors::LinkerUnsupportedModifier); |
17df50a5 XL |
443 | } |
444 | } | |
e1599b0c | 445 | self.hint_dynamic(); |
2b03887a | 446 | self.cmd.arg(format!("-l{}{lib}", if verbatim && self.is_gnu { ":" } else { "" },)); |
17df50a5 XL |
447 | if !as_needed { |
448 | if self.sess.target.is_like_osx { | |
449 | // See above FIXME comment | |
2b03887a | 450 | } else if self.is_gnu && !self.sess.target.is_like_windows { |
17df50a5 XL |
451 | self.linker_arg("--as-needed"); |
452 | } | |
453 | } | |
e1599b0c | 454 | } |
064997fb | 455 | fn link_staticlib(&mut self, lib: &str, verbatim: bool) { |
e1599b0c | 456 | self.hint_static(); |
2b03887a | 457 | self.cmd.arg(format!("-l{}{lib}", if verbatim && self.is_gnu { ":" } else { "" },)); |
8faf50e0 | 458 | } |
dfeec247 XL |
459 | fn link_rlib(&mut self, lib: &Path) { |
460 | self.hint_static(); | |
461 | self.cmd.arg(lib); | |
462 | } | |
463 | fn include_path(&mut self, path: &Path) { | |
464 | self.cmd.arg("-L").arg(path); | |
465 | } | |
466 | fn framework_path(&mut self, path: &Path) { | |
467 | self.cmd.arg("-F").arg(path); | |
468 | } | |
469 | fn output_filename(&mut self, path: &Path) { | |
470 | self.cmd.arg("-o").arg(path); | |
471 | } | |
472 | fn add_object(&mut self, path: &Path) { | |
473 | self.cmd.arg(path); | |
474 | } | |
dfeec247 | 475 | fn full_relro(&mut self) { |
9ffffee4 | 476 | self.linker_args(&["-z", "relro", "-z", "now"]); |
dfeec247 XL |
477 | } |
478 | fn partial_relro(&mut self) { | |
9ffffee4 | 479 | self.linker_args(&["-z", "relro"]); |
dfeec247 XL |
480 | } |
481 | fn no_relro(&mut self) { | |
9ffffee4 | 482 | self.linker_args(&["-z", "norelro"]); |
dfeec247 | 483 | } |
62682a34 | 484 | |
064997fb | 485 | fn link_rust_dylib(&mut self, lib: &str, _path: &Path) { |
cc61c64b | 486 | self.hint_dynamic(); |
a1dfa0c6 | 487 | self.cmd.arg(format!("-l{}", lib)); |
c1a9b12d SL |
488 | } |
489 | ||
064997fb | 490 | fn link_framework(&mut self, framework: &str, as_needed: bool) { |
cc61c64b | 491 | self.hint_dynamic(); |
17df50a5 XL |
492 | if !as_needed { |
493 | // FIXME(81490): ld64 as of macOS 11 supports the -needed_framework | |
494 | // flag but we have no way to detect that here. | |
064997fb | 495 | // self.cmd.arg("-needed_framework").arg(framework); |
2b03887a | 496 | self.sess.emit_warning(errors::Ld64UnimplementedModifier); |
17df50a5 | 497 | } |
064997fb | 498 | self.cmd.arg("-framework").arg(framework); |
62682a34 SL |
499 | } |
500 | ||
cc61c64b XL |
501 | // Here we explicitly ask that the entire archive is included into the |
502 | // result artifact. For more details see #15460, but the gist is that | |
503 | // the linker will strip away any unused objects in the archive if we | |
504 | // don't otherwise explicitly reference them. This can occur for | |
505 | // libraries which are just providing bindings, libraries with generic | |
506 | // functions, etc. | |
064997fb | 507 | fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, search_path: &[PathBuf]) { |
cc61c64b | 508 | self.hint_static(); |
29967ef6 XL |
509 | let target = &self.sess.target; |
510 | if !target.is_like_osx { | |
2b03887a FG |
511 | self.linker_arg("--whole-archive"); |
512 | self.cmd.arg(format!("-l{}{lib}", if verbatim && self.is_gnu { ":" } else { "" },)); | |
cc61c64b | 513 | self.linker_arg("--no-whole-archive"); |
62682a34 | 514 | } else { |
cc61c64b | 515 | // -force_load is the macOS equivalent of --whole-archive, but it |
62682a34 | 516 | // involves passing the full path to the library to link. |
8faf50e0 | 517 | self.linker_arg("-force_load"); |
487cf647 | 518 | let lib = find_native_static_library(lib, verbatim, search_path, &self.sess); |
8faf50e0 | 519 | self.linker_arg(&lib); |
c1a9b12d SL |
520 | } |
521 | } | |
522 | ||
523 | fn link_whole_rlib(&mut self, lib: &Path) { | |
cc61c64b | 524 | self.hint_static(); |
29967ef6 | 525 | if self.sess.target.is_like_osx { |
8faf50e0 XL |
526 | self.linker_arg("-force_load"); |
527 | self.linker_arg(&lib); | |
c1a9b12d | 528 | } else { |
cc61c64b XL |
529 | self.linker_arg("--whole-archive").cmd.arg(lib); |
530 | self.linker_arg("--no-whole-archive"); | |
62682a34 SL |
531 | } |
532 | } | |
533 | ||
a7813a04 | 534 | fn gc_sections(&mut self, keep_metadata: bool) { |
62682a34 SL |
535 | // The dead_strip option to the linker specifies that functions and data |
536 | // unreachable by the entry point will be removed. This is quite useful | |
537 | // with Rust's compilation model of compiling libraries at a time into | |
538 | // one object file. For example, this brings hello world from 1.7MB to | |
539 | // 458K. | |
540 | // | |
541 | // Note that this is done for both executables and dynamic libraries. We | |
542 | // won't get much benefit from dylibs because LLVM will have already | |
543 | // stripped away as much as it could. This has not been seen to impact | |
544 | // link times negatively. | |
545 | // | |
546 | // -dead_strip can't be part of the pre_link_args because it's also used | |
9c376795 | 547 | // for partial linking when using multiple codegen units (-r). So we |
62682a34 | 548 | // insert it here. |
29967ef6 | 549 | if self.sess.target.is_like_osx { |
cc61c64b | 550 | self.linker_arg("-dead_strip"); |
62682a34 SL |
551 | |
552 | // If we're building a dylib, we don't use --gc-sections because LLVM | |
553 | // has already done the best it can do, and we also don't want to | |
554 | // eliminate the metadata. If we're building an executable, however, | |
555 | // --gc-sections drops the size of hello world from 1.8MB to 597K, a 67% | |
556 | // reduction. | |
2b03887a | 557 | } else if (self.is_gnu || self.sess.target.is_like_wasm) && !keep_metadata { |
cc61c64b | 558 | self.linker_arg("--gc-sections"); |
62682a34 SL |
559 | } |
560 | } | |
561 | ||
17df50a5 | 562 | fn no_gc_sections(&mut self) { |
2b03887a | 563 | if self.is_gnu || self.sess.target.is_like_wasm { |
17df50a5 XL |
564 | self.linker_arg("--no-gc-sections"); |
565 | } | |
566 | } | |
567 | ||
62682a34 | 568 | fn optimize(&mut self) { |
2b03887a | 569 | if !self.is_gnu && !self.sess.target.is_like_wasm { |
dfeec247 XL |
570 | return; |
571 | } | |
62682a34 SL |
572 | |
573 | // GNU-style linkers support optimization with -O. GNU ld doesn't | |
574 | // need a numeric argument, but other linkers do. | |
dfeec247 XL |
575 | if self.sess.opts.optimize == config::OptLevel::Default |
576 | || self.sess.opts.optimize == config::OptLevel::Aggressive | |
577 | { | |
cc61c64b | 578 | self.linker_arg("-O1"); |
62682a34 SL |
579 | } |
580 | } | |
581 | ||
0531ce1d | 582 | fn pgo_gen(&mut self) { |
2b03887a | 583 | if !self.is_gnu { |
dfeec247 XL |
584 | return; |
585 | } | |
0531ce1d XL |
586 | |
587 | // If we're doing PGO generation stuff and on a GNU-like linker, use the | |
588 | // "-u" flag to properly pull in the profiler runtime bits. | |
589 | // | |
590 | // This is because LLVM otherwise won't add the needed initialization | |
591 | // for us on Linux (though the extra flag should be harmless if it | |
592 | // does). | |
593 | // | |
594 | // See https://reviews.llvm.org/D14033 and https://reviews.llvm.org/D14030. | |
595 | // | |
596 | // Though it may be worth to try to revert those changes upstream, since | |
597 | // the overhead of the initialization should be minor. | |
598 | self.cmd.arg("-u"); | |
599 | self.cmd.arg("__llvm_profile_runtime"); | |
600 | } | |
601 | ||
f035d41b | 602 | fn control_flow_guard(&mut self) {} |
74b04a01 | 603 | |
04454e1e | 604 | fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) { |
17df50a5 XL |
605 | // MacOS linker doesn't support stripping symbols directly anymore. |
606 | if self.sess.target.is_like_osx { | |
607 | return; | |
608 | } | |
609 | ||
f9f354fc XL |
610 | match strip { |
611 | Strip::None => {} | |
612 | Strip::Debuginfo => { | |
2b03887a FG |
613 | // The illumos linker does not support --strip-debug although |
614 | // it does support --strip-all as a compatibility alias for -s. | |
615 | // The --strip-debug case is handled by running an external | |
616 | // `strip` utility as a separate step after linking. | |
617 | if self.sess.target.os != "illumos" { | |
618 | self.linker_arg("--strip-debug"); | |
619 | } | |
a1dfa0c6 | 620 | } |
f9f354fc | 621 | Strip::Symbols => { |
17df50a5 | 622 | self.linker_arg("--strip-all"); |
f9f354fc XL |
623 | } |
624 | } | |
c1a9b12d SL |
625 | } |
626 | ||
f9f354fc | 627 | fn no_crt_objects(&mut self) { |
cc61c64b | 628 | if !self.is_ld { |
f9f354fc | 629 | self.cmd.arg("-nostartfiles"); |
cc61c64b | 630 | } |
62682a34 SL |
631 | } |
632 | ||
f9f354fc XL |
633 | fn no_default_libraries(&mut self) { |
634 | if !self.is_ld { | |
635 | self.cmd.arg("-nodefaultlibs"); | |
62682a34 SL |
636 | } |
637 | } | |
638 | ||
136023e0 | 639 | fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) { |
48663c56 | 640 | // Symbol visibility in object files typically takes care of this. |
064997fb FG |
641 | if crate_type == CrateType::Executable { |
642 | let should_export_executable_symbols = | |
643 | self.sess.opts.unstable_opts.export_executable_symbols; | |
644 | if self.sess.target.override_export_symbols.is_none() | |
645 | && !should_export_executable_symbols | |
646 | { | |
647 | return; | |
648 | } | |
48663c56 XL |
649 | } |
650 | ||
dc9dc135 XL |
651 | // We manually create a list of exported symbols to ensure we don't expose any more. |
652 | // The object files have far more public symbols than we actually want to export, | |
653 | // so we hide them all here. | |
a7813a04 | 654 | |
29967ef6 | 655 | if !self.sess.target.limit_rdylib_exports { |
532ac7d7 XL |
656 | return; |
657 | } | |
658 | ||
064997fb | 659 | // FIXME(#99978) hide #[no_mangle] symbols for proc-macros |
dc9dc135 | 660 | |
29967ef6 | 661 | let is_windows = self.sess.target.is_like_windows; |
3dfed10e | 662 | let path = tmpdir.join(if is_windows { "list.def" } else { "list" }); |
9e0c209e | 663 | |
476ff2be SL |
664 | debug!("EXPORTED SYMBOLS:"); |
665 | ||
29967ef6 | 666 | if self.sess.target.is_like_osx { |
476ff2be | 667 | // Write a plain, newline-separated list of symbols |
532ac7d7 | 668 | let res: io::Result<()> = try { |
9e0c209e | 669 | let mut f = BufWriter::new(File::create(&path)?); |
136023e0 | 670 | for sym in symbols { |
476ff2be SL |
671 | debug!(" _{}", sym); |
672 | writeln!(f, "_{}", sym)?; | |
9e0c209e | 673 | } |
532ac7d7 | 674 | }; |
2b03887a FG |
675 | if let Err(error) = res { |
676 | self.sess.emit_fatal(errors::LibDefWriteFailure { error }); | |
a7813a04 | 677 | } |
3dfed10e XL |
678 | } else if is_windows { |
679 | let res: io::Result<()> = try { | |
680 | let mut f = BufWriter::new(File::create(&path)?); | |
681 | ||
682 | // .def file similar to MSVC one but without LIBRARY section | |
683 | // because LD doesn't like when it's empty | |
684 | writeln!(f, "EXPORTS")?; | |
136023e0 | 685 | for symbol in symbols { |
3dfed10e XL |
686 | debug!(" _{}", symbol); |
687 | writeln!(f, " {}", symbol)?; | |
688 | } | |
689 | }; | |
2b03887a FG |
690 | if let Err(error) = res { |
691 | self.sess.emit_fatal(errors::LibDefWriteFailure { error }); | |
3dfed10e | 692 | } |
a7813a04 | 693 | } else { |
476ff2be | 694 | // Write an LD version script |
532ac7d7 | 695 | let res: io::Result<()> = try { |
9e0c209e | 696 | let mut f = BufWriter::new(File::create(&path)?); |
e1599b0c | 697 | writeln!(f, "{{")?; |
136023e0 | 698 | if !symbols.is_empty() { |
e1599b0c | 699 | writeln!(f, " global:")?; |
136023e0 | 700 | for sym in symbols { |
e1599b0c XL |
701 | debug!(" {};", sym); |
702 | writeln!(f, " {};", sym)?; | |
703 | } | |
9e0c209e | 704 | } |
476ff2be | 705 | writeln!(f, "\n local:\n *;\n}};")?; |
532ac7d7 | 706 | }; |
2b03887a FG |
707 | if let Err(error) = res { |
708 | self.sess.emit_fatal(errors::VersionScriptWriteFailure { error }); | |
9e0c209e | 709 | } |
a7813a04 | 710 | } |
9e0c209e | 711 | |
29967ef6 | 712 | if self.sess.target.is_like_osx { |
3c0e092e | 713 | self.linker_args(&[OsString::from("-exported_symbols_list"), path.into()]); |
29967ef6 | 714 | } else if self.sess.target.is_like_solaris { |
3c0e092e | 715 | self.linker_args(&[OsString::from("-M"), path.into()]); |
476ff2be | 716 | } else { |
3c0e092e XL |
717 | if is_windows { |
718 | self.linker_arg(path); | |
719 | } else { | |
720 | let mut arg = OsString::from("--version-script="); | |
721 | arg.push(path); | |
722 | self.linker_arg(arg); | |
3dfed10e | 723 | } |
476ff2be | 724 | } |
e9174d1e | 725 | } |
c30ab7b3 SL |
726 | |
727 | fn subsystem(&mut self, subsystem: &str) { | |
8faf50e0 XL |
728 | self.linker_arg("--subsystem"); |
729 | self.linker_arg(&subsystem); | |
cc61c64b XL |
730 | } |
731 | ||
17df50a5 | 732 | fn reset_per_library_state(&mut self) { |
cc61c64b | 733 | self.hint_dynamic(); // Reset to default before returning the composed command line. |
c30ab7b3 | 734 | } |
0531ce1d | 735 | |
9fa01778 XL |
736 | fn linker_plugin_lto(&mut self) { |
737 | match self.sess.opts.cg.linker_plugin_lto { | |
738 | LinkerPluginLto::Disabled => { | |
94b46f34 XL |
739 | // Nothing to do |
740 | } | |
9fa01778 XL |
741 | LinkerPluginLto::LinkerPluginAuto => { |
742 | self.push_linker_plugin_lto_args(None); | |
8faf50e0 | 743 | } |
9fa01778 XL |
744 | LinkerPluginLto::LinkerPlugin(ref path) => { |
745 | self.push_linker_plugin_lto_args(Some(path.as_os_str())); | |
94b46f34 XL |
746 | } |
747 | } | |
748 | } | |
f035d41b XL |
749 | |
750 | // Add the `GNU_EH_FRAME` program header which is required to locate unwinding information. | |
751 | // Some versions of `gcc` add it implicitly, some (e.g. `musl-gcc`) don't, | |
752 | // so we just always add it. | |
753 | fn add_eh_frame_header(&mut self) { | |
f9652781 | 754 | self.linker_arg("--eh-frame-hdr"); |
f035d41b | 755 | } |
cdc7bbd5 XL |
756 | |
757 | fn add_no_exec(&mut self) { | |
758 | if self.sess.target.is_like_windows { | |
759 | self.linker_arg("--nxcompat"); | |
2b03887a | 760 | } else if self.is_gnu { |
9ffffee4 | 761 | self.linker_args(&["-z", "noexecstack"]); |
cdc7bbd5 XL |
762 | } |
763 | } | |
764 | ||
765 | fn add_as_needed(&mut self) { | |
2b03887a | 766 | if self.is_gnu && !self.sess.target.is_like_windows { |
cdc7bbd5 | 767 | self.linker_arg("--as-needed"); |
17df50a5 XL |
768 | } else if self.sess.target.is_like_solaris { |
769 | // -z ignore is the Solaris equivalent to the GNU ld --as-needed option | |
3c0e092e | 770 | self.linker_args(&["-z", "ignore"]); |
cdc7bbd5 XL |
771 | } |
772 | } | |
62682a34 SL |
773 | } |
774 | ||
775 | pub struct MsvcLinker<'a> { | |
cc61c64b | 776 | cmd: Command, |
3157f602 | 777 | sess: &'a Session, |
62682a34 SL |
778 | } |
779 | ||
780 | impl<'a> Linker for MsvcLinker<'a> { | |
ba9703b0 XL |
781 | fn cmd(&mut self) -> &mut Command { |
782 | &mut self.cmd | |
783 | } | |
f9f354fc XL |
784 | |
785 | fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) { | |
786 | match output_kind { | |
787 | LinkOutputKind::DynamicNoPicExe | |
788 | | LinkOutputKind::DynamicPicExe | |
789 | | LinkOutputKind::StaticNoPicExe | |
790 | | LinkOutputKind::StaticPicExe => {} | |
791 | LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => { | |
792 | self.cmd.arg("/DLL"); | |
793 | let mut arg: OsString = "/IMPLIB:".into(); | |
794 | arg.push(out_filename.with_extension("dll.lib")); | |
795 | self.cmd.arg(arg); | |
796 | } | |
5869c6ff XL |
797 | LinkOutputKind::WasiReactorExe => { |
798 | panic!("can't link as reactor on non-wasi target"); | |
799 | } | |
f9f354fc XL |
800 | } |
801 | } | |
802 | ||
dfeec247 XL |
803 | fn link_rlib(&mut self, lib: &Path) { |
804 | self.cmd.arg(lib); | |
805 | } | |
806 | fn add_object(&mut self, path: &Path) { | |
807 | self.cmd.arg(path); | |
808 | } | |
7453a54e | 809 | |
a7813a04 | 810 | fn gc_sections(&mut self, _keep_metadata: bool) { |
476ff2be SL |
811 | // MSVC's ICF (Identical COMDAT Folding) link optimization is |
812 | // slow for Rust and thus we disable it by default when not in | |
813 | // optimization build. | |
814 | if self.sess.opts.optimize != config::OptLevel::No { | |
815 | self.cmd.arg("/OPT:REF,ICF"); | |
816 | } else { | |
817 | // It is necessary to specify NOICF here, because /OPT:REF | |
818 | // implies ICF by default. | |
819 | self.cmd.arg("/OPT:REF,NOICF"); | |
820 | } | |
a7813a04 | 821 | } |
62682a34 | 822 | |
17df50a5 XL |
823 | fn no_gc_sections(&mut self) { |
824 | self.cmd.arg("/OPT:NOREF,NOICF"); | |
825 | } | |
826 | ||
064997fb | 827 | fn link_dylib(&mut self, lib: &str, verbatim: bool, _as_needed: bool) { |
17df50a5 | 828 | self.cmd.arg(format!("{}{}", lib, if verbatim { "" } else { ".lib" })); |
62682a34 | 829 | } |
c1a9b12d | 830 | |
064997fb | 831 | fn link_rust_dylib(&mut self, lib: &str, path: &Path) { |
c1a9b12d SL |
832 | // When producing a dll, the MSVC linker may not actually emit a |
833 | // `foo.lib` file if the dll doesn't actually export any symbols, so we | |
834 | // check to see if the file is there and just omit linking to it if it's | |
835 | // not present. | |
7453a54e | 836 | let name = format!("{}.dll.lib", lib); |
17df50a5 | 837 | if path.join(&name).exists() { |
c1a9b12d SL |
838 | self.cmd.arg(name); |
839 | } | |
840 | } | |
841 | ||
064997fb | 842 | fn link_staticlib(&mut self, lib: &str, verbatim: bool) { |
17df50a5 | 843 | self.cmd.arg(format!("{}{}", lib, if verbatim { "" } else { ".lib" })); |
62682a34 SL |
844 | } |
845 | ||
3b2f2976 XL |
846 | fn full_relro(&mut self) { |
847 | // noop | |
848 | } | |
849 | ||
0531ce1d XL |
850 | fn partial_relro(&mut self) { |
851 | // noop | |
852 | } | |
853 | ||
854 | fn no_relro(&mut self) { | |
855 | // noop | |
856 | } | |
857 | ||
f9f354fc XL |
858 | fn no_crt_objects(&mut self) { |
859 | // noop | |
860 | } | |
861 | ||
62682a34 | 862 | fn no_default_libraries(&mut self) { |
ba9703b0 | 863 | self.cmd.arg("/NODEFAULTLIB"); |
62682a34 SL |
864 | } |
865 | ||
866 | fn include_path(&mut self, path: &Path) { | |
867 | let mut arg = OsString::from("/LIBPATH:"); | |
868 | arg.push(path); | |
869 | self.cmd.arg(&arg); | |
870 | } | |
871 | ||
872 | fn output_filename(&mut self, path: &Path) { | |
873 | let mut arg = OsString::from("/OUT:"); | |
874 | arg.push(path); | |
875 | self.cmd.arg(&arg); | |
876 | } | |
877 | ||
878 | fn framework_path(&mut self, _path: &Path) { | |
54a0048b | 879 | bug!("frameworks are not supported on windows") |
62682a34 | 880 | } |
064997fb | 881 | fn link_framework(&mut self, _framework: &str, _as_needed: bool) { |
54a0048b | 882 | bug!("frameworks are not supported on windows") |
62682a34 SL |
883 | } |
884 | ||
064997fb | 885 | fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, _search_path: &[PathBuf]) { |
17df50a5 | 886 | self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", lib, if verbatim { "" } else { ".lib" })); |
62682a34 | 887 | } |
c1a9b12d | 888 | fn link_whole_rlib(&mut self, path: &Path) { |
f035d41b XL |
889 | let mut arg = OsString::from("/WHOLEARCHIVE:"); |
890 | arg.push(path); | |
891 | self.cmd.arg(arg); | |
c1a9b12d | 892 | } |
62682a34 SL |
893 | fn optimize(&mut self) { |
894 | // Needs more investigation of `/OPT` arguments | |
895 | } | |
c1a9b12d | 896 | |
0531ce1d XL |
897 | fn pgo_gen(&mut self) { |
898 | // Nothing needed here. | |
899 | } | |
900 | ||
74b04a01 XL |
901 | fn control_flow_guard(&mut self) { |
902 | self.cmd.arg("/guard:cf"); | |
903 | } | |
904 | ||
923072b8 | 905 | fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]) { |
f9f354fc XL |
906 | match strip { |
907 | Strip::None => { | |
908 | // This will cause the Microsoft linker to generate a PDB file | |
909 | // from the CodeView line tables in the object files. | |
910 | self.cmd.arg("/DEBUG"); | |
911 | ||
912 | // This will cause the Microsoft linker to embed .natvis info into the PDB file | |
913 | let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc"); | |
914 | if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) { | |
915 | for entry in natvis_dir { | |
916 | match entry { | |
917 | Ok(entry) => { | |
918 | let path = entry.path(); | |
919 | if path.extension() == Some("natvis".as_ref()) { | |
920 | let mut arg = OsString::from("/NATVIS:"); | |
921 | arg.push(path); | |
922 | self.cmd.arg(arg); | |
923 | } | |
924 | } | |
2b03887a FG |
925 | Err(error) => { |
926 | self.sess.emit_warning(errors::NoNatvisDirectory { error }); | |
f9f354fc | 927 | } |
3b2f2976 | 928 | } |
dfeec247 | 929 | } |
3b2f2976 | 930 | } |
04454e1e FG |
931 | |
932 | // This will cause the Microsoft linker to embed .natvis info for all crates into the PDB file | |
923072b8 | 933 | for path in natvis_debugger_visualizers { |
04454e1e FG |
934 | let mut arg = OsString::from("/NATVIS:"); |
935 | arg.push(path); | |
936 | self.cmd.arg(arg); | |
937 | } | |
3b2f2976 | 938 | } |
f9f354fc XL |
939 | Strip::Debuginfo | Strip::Symbols => { |
940 | self.cmd.arg("/DEBUG:NONE"); | |
941 | } | |
3b2f2976 | 942 | } |
c1a9b12d SL |
943 | } |
944 | ||
e9174d1e SL |
945 | // Currently the compiler doesn't use `dllexport` (an LLVM attribute) to |
946 | // export symbols from a dynamic library. When building a dynamic library, | |
947 | // however, we're going to want some symbols exported, so this function | |
948 | // generates a DEF file which lists all the symbols. | |
949 | // | |
950 | // The linker will read this `*.def` file and export all the symbols from | |
951 | // the dynamic library. Note that this is not as simple as just exporting | |
94b46f34 | 952 | // all the symbols in the current crate (as specified by `codegen.reachable`) |
e9174d1e SL |
953 | // but rather we also need to possibly export the symbols of upstream |
954 | // crates. Upstream rlibs may be linked statically to this dynamic library, | |
955 | // in which case they may continue to transitively be used and hence need | |
956 | // their symbols exported. | |
136023e0 | 957 | fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) { |
48663c56 XL |
958 | // Symbol visibility takes care of this typically |
959 | if crate_type == CrateType::Executable { | |
064997fb FG |
960 | let should_export_executable_symbols = |
961 | self.sess.opts.unstable_opts.export_executable_symbols; | |
962 | if !should_export_executable_symbols { | |
963 | return; | |
964 | } | |
48663c56 XL |
965 | } |
966 | ||
e9174d1e | 967 | let path = tmpdir.join("lib.def"); |
532ac7d7 | 968 | let res: io::Result<()> = try { |
54a0048b | 969 | let mut f = BufWriter::new(File::create(&path)?); |
e9174d1e SL |
970 | |
971 | // Start off with the standard module name header and then go | |
972 | // straight to exports. | |
54a0048b SL |
973 | writeln!(f, "LIBRARY")?; |
974 | writeln!(f, "EXPORTS")?; | |
136023e0 | 975 | for symbol in symbols { |
8bb4bdeb | 976 | debug!(" _{}", symbol); |
3157f602 | 977 | writeln!(f, " {}", symbol)?; |
e9174d1e | 978 | } |
532ac7d7 | 979 | }; |
2b03887a FG |
980 | if let Err(error) = res { |
981 | self.sess.emit_fatal(errors::LibDefWriteFailure { error }); | |
e9174d1e SL |
982 | } |
983 | let mut arg = OsString::from("/DEF:"); | |
984 | arg.push(path); | |
985 | self.cmd.arg(&arg); | |
986 | } | |
c30ab7b3 SL |
987 | |
988 | fn subsystem(&mut self, subsystem: &str) { | |
989 | // Note that previous passes of the compiler validated this subsystem, | |
990 | // so we just blindly pass it to the linker. | |
991 | self.cmd.arg(&format!("/SUBSYSTEM:{}", subsystem)); | |
992 | ||
993 | // Windows has two subsystems we're interested in right now, the console | |
994 | // and windows subsystems. These both implicitly have different entry | |
995 | // points (starting symbols). The console entry point starts with | |
996 | // `mainCRTStartup` and the windows entry point starts with | |
997 | // `WinMainCRTStartup`. These entry points, defined in system libraries, | |
998 | // will then later probe for either `main` or `WinMain`, respectively to | |
999 | // start the application. | |
1000 | // | |
1001 | // In Rust we just always generate a `main` function so we want control | |
1002 | // to always start there, so we force the entry point on the windows | |
1003 | // subsystem to be `mainCRTStartup` to get everything booted up | |
1004 | // correctly. | |
1005 | // | |
1006 | // For more information see RFC #1665 | |
1007 | if subsystem == "windows" { | |
1008 | self.cmd.arg("/ENTRY:mainCRTStartup"); | |
1009 | } | |
1010 | } | |
cc61c64b | 1011 | |
9fa01778 | 1012 | fn linker_plugin_lto(&mut self) { |
94b46f34 XL |
1013 | // Do nothing |
1014 | } | |
cdc7bbd5 XL |
1015 | |
1016 | fn add_no_exec(&mut self) { | |
1017 | self.cmd.arg("/NXCOMPAT"); | |
1018 | } | |
62682a34 | 1019 | } |
a7813a04 | 1020 | |
8bb4bdeb | 1021 | pub struct EmLinker<'a> { |
cc61c64b | 1022 | cmd: Command, |
8bb4bdeb | 1023 | sess: &'a Session, |
8bb4bdeb XL |
1024 | } |
1025 | ||
1026 | impl<'a> Linker for EmLinker<'a> { | |
ba9703b0 XL |
1027 | fn cmd(&mut self) -> &mut Command { |
1028 | &mut self.cmd | |
1029 | } | |
f9f354fc XL |
1030 | |
1031 | fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {} | |
1032 | ||
8bb4bdeb XL |
1033 | fn include_path(&mut self, path: &Path) { |
1034 | self.cmd.arg("-L").arg(path); | |
1035 | } | |
1036 | ||
064997fb FG |
1037 | fn link_staticlib(&mut self, lib: &str, _verbatim: bool) { |
1038 | self.cmd.arg("-l").arg(lib); | |
8bb4bdeb XL |
1039 | } |
1040 | ||
1041 | fn output_filename(&mut self, path: &Path) { | |
1042 | self.cmd.arg("-o").arg(path); | |
1043 | } | |
1044 | ||
1045 | fn add_object(&mut self, path: &Path) { | |
1046 | self.cmd.arg(path); | |
1047 | } | |
1048 | ||
064997fb | 1049 | fn link_dylib(&mut self, lib: &str, verbatim: bool, _as_needed: bool) { |
8bb4bdeb | 1050 | // Emscripten always links statically |
17df50a5 | 1051 | self.link_staticlib(lib, verbatim); |
8bb4bdeb XL |
1052 | } |
1053 | ||
064997fb | 1054 | fn link_whole_staticlib(&mut self, lib: &str, verbatim: bool, _search_path: &[PathBuf]) { |
8bb4bdeb | 1055 | // not supported? |
17df50a5 | 1056 | self.link_staticlib(lib, verbatim); |
8bb4bdeb XL |
1057 | } |
1058 | ||
1059 | fn link_whole_rlib(&mut self, lib: &Path) { | |
1060 | // not supported? | |
1061 | self.link_rlib(lib); | |
1062 | } | |
1063 | ||
064997fb | 1064 | fn link_rust_dylib(&mut self, lib: &str, _path: &Path) { |
17df50a5 | 1065 | self.link_dylib(lib, false, true); |
8bb4bdeb XL |
1066 | } |
1067 | ||
1068 | fn link_rlib(&mut self, lib: &Path) { | |
1069 | self.add_object(lib); | |
1070 | } | |
1071 | ||
3b2f2976 XL |
1072 | fn full_relro(&mut self) { |
1073 | // noop | |
1074 | } | |
1075 | ||
0531ce1d XL |
1076 | fn partial_relro(&mut self) { |
1077 | // noop | |
1078 | } | |
1079 | ||
1080 | fn no_relro(&mut self) { | |
1081 | // noop | |
1082 | } | |
1083 | ||
8bb4bdeb XL |
1084 | fn framework_path(&mut self, _path: &Path) { |
1085 | bug!("frameworks are not supported on Emscripten") | |
1086 | } | |
1087 | ||
064997fb | 1088 | fn link_framework(&mut self, _framework: &str, _as_needed: bool) { |
8bb4bdeb XL |
1089 | bug!("frameworks are not supported on Emscripten") |
1090 | } | |
1091 | ||
1092 | fn gc_sections(&mut self, _keep_metadata: bool) { | |
1093 | // noop | |
1094 | } | |
1095 | ||
17df50a5 XL |
1096 | fn no_gc_sections(&mut self) { |
1097 | // noop | |
1098 | } | |
1099 | ||
8bb4bdeb XL |
1100 | fn optimize(&mut self) { |
1101 | // Emscripten performs own optimizations | |
1102 | self.cmd.arg(match self.sess.opts.optimize { | |
1103 | OptLevel::No => "-O0", | |
1104 | OptLevel::Less => "-O1", | |
1105 | OptLevel::Default => "-O2", | |
1106 | OptLevel::Aggressive => "-O3", | |
1107 | OptLevel::Size => "-Os", | |
dfeec247 | 1108 | OptLevel::SizeMin => "-Oz", |
8bb4bdeb | 1109 | }); |
8bb4bdeb XL |
1110 | } |
1111 | ||
0531ce1d XL |
1112 | fn pgo_gen(&mut self) { |
1113 | // noop, but maybe we need something like the gnu linker? | |
1114 | } | |
1115 | ||
f035d41b | 1116 | fn control_flow_guard(&mut self) {} |
74b04a01 | 1117 | |
04454e1e | 1118 | fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) { |
8bb4bdeb XL |
1119 | // Preserve names or generate source maps depending on debug info |
1120 | self.cmd.arg(match self.sess.opts.debuginfo { | |
b7449926 | 1121 | DebugInfo::None => "-g0", |
923072b8 FG |
1122 | DebugInfo::Limited => "--profiling-funcs", |
1123 | DebugInfo::Full => "-g", | |
8bb4bdeb XL |
1124 | }); |
1125 | } | |
1126 | ||
f9f354fc XL |
1127 | fn no_crt_objects(&mut self) {} |
1128 | ||
8bb4bdeb | 1129 | fn no_default_libraries(&mut self) { |
923072b8 | 1130 | self.cmd.arg("-nodefaultlibs"); |
8bb4bdeb XL |
1131 | } |
1132 | ||
136023e0 | 1133 | fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { |
8bb4bdeb XL |
1134 | debug!("EXPORTED SYMBOLS:"); |
1135 | ||
1136 | self.cmd.arg("-s"); | |
1137 | ||
1138 | let mut arg = OsString::from("EXPORTED_FUNCTIONS="); | |
923072b8 FG |
1139 | let encoded = serde_json::to_string( |
1140 | &symbols.iter().map(|sym| "_".to_owned() + sym).collect::<Vec<_>>(), | |
1141 | ) | |
1142 | .unwrap(); | |
8bb4bdeb | 1143 | debug!("{}", encoded); |
923072b8 | 1144 | |
8bb4bdeb XL |
1145 | arg.push(encoded); |
1146 | ||
1147 | self.cmd.arg(arg); | |
1148 | } | |
1149 | ||
1150 | fn subsystem(&mut self, _subsystem: &str) { | |
1151 | // noop | |
1152 | } | |
cc61c64b | 1153 | |
9fa01778 | 1154 | fn linker_plugin_lto(&mut self) { |
94b46f34 XL |
1155 | // Do nothing |
1156 | } | |
8bb4bdeb XL |
1157 | } |
1158 | ||
8faf50e0 | 1159 | pub struct WasmLd<'a> { |
0531ce1d | 1160 | cmd: Command, |
8faf50e0 | 1161 | sess: &'a Session, |
0531ce1d XL |
1162 | } |
1163 | ||
0731742a | 1164 | impl<'a> WasmLd<'a> { |
136023e0 | 1165 | fn new(mut cmd: Command, sess: &'a Session) -> WasmLd<'a> { |
416331ca XL |
1166 | // If the atomics feature is enabled for wasm then we need a whole bunch |
1167 | // of flags: | |
1168 | // | |
1169 | // * `--shared-memory` - the link won't even succeed without this, flags | |
1170 | // the one linear memory as `shared` | |
1171 | // | |
1172 | // * `--max-memory=1G` - when specifying a shared memory this must also | |
1173 | // be specified. We conservatively choose 1GB but users should be able | |
1174 | // to override this with `-C link-arg`. | |
1175 | // | |
1176 | // * `--import-memory` - it doesn't make much sense for memory to be | |
1177 | // exported in a threaded module because typically you're | |
1178 | // sharing memory and instantiating the module multiple times. As a | |
1179 | // result if it were exported then we'd just have no sharing. | |
1180 | // | |
2b03887a FG |
1181 | // On wasm32-unknown-unknown, we also export symbols for glue code to use: |
1182 | // * `--export=*tls*` - when `#[thread_local]` symbols are used these | |
1183 | // symbols are how the TLS segments are initialized and configured. | |
f035d41b | 1184 | if sess.target_features.contains(&sym::atomics) { |
416331ca XL |
1185 | cmd.arg("--shared-memory"); |
1186 | cmd.arg("--max-memory=1073741824"); | |
1187 | cmd.arg("--import-memory"); | |
2b03887a FG |
1188 | if sess.target.os == "unknown" { |
1189 | cmd.arg("--export=__wasm_init_tls"); | |
1190 | cmd.arg("--export=__tls_size"); | |
1191 | cmd.arg("--export=__tls_align"); | |
1192 | cmd.arg("--export=__tls_base"); | |
1193 | } | |
416331ca | 1194 | } |
136023e0 | 1195 | WasmLd { cmd, sess } |
0731742a XL |
1196 | } |
1197 | } | |
1198 | ||
8faf50e0 | 1199 | impl<'a> Linker for WasmLd<'a> { |
ba9703b0 XL |
1200 | fn cmd(&mut self) -> &mut Command { |
1201 | &mut self.cmd | |
1202 | } | |
1203 | ||
f9f354fc XL |
1204 | fn set_output_kind(&mut self, output_kind: LinkOutputKind, _out_filename: &Path) { |
1205 | match output_kind { | |
1206 | LinkOutputKind::DynamicNoPicExe | |
1207 | | LinkOutputKind::DynamicPicExe | |
1208 | | LinkOutputKind::StaticNoPicExe | |
1209 | | LinkOutputKind::StaticPicExe => {} | |
1210 | LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => { | |
1211 | self.cmd.arg("--no-entry"); | |
1212 | } | |
5869c6ff XL |
1213 | LinkOutputKind::WasiReactorExe => { |
1214 | self.cmd.arg("--entry"); | |
1215 | self.cmd.arg("_initialize"); | |
1216 | } | |
f9f354fc XL |
1217 | } |
1218 | } | |
1219 | ||
064997fb FG |
1220 | fn link_dylib(&mut self, lib: &str, _verbatim: bool, _as_needed: bool) { |
1221 | self.cmd.arg("-l").arg(lib); | |
0531ce1d XL |
1222 | } |
1223 | ||
064997fb FG |
1224 | fn link_staticlib(&mut self, lib: &str, _verbatim: bool) { |
1225 | self.cmd.arg("-l").arg(lib); | |
0531ce1d XL |
1226 | } |
1227 | ||
1228 | fn link_rlib(&mut self, lib: &Path) { | |
1229 | self.cmd.arg(lib); | |
1230 | } | |
1231 | ||
1232 | fn include_path(&mut self, path: &Path) { | |
1233 | self.cmd.arg("-L").arg(path); | |
1234 | } | |
1235 | ||
1236 | fn framework_path(&mut self, _path: &Path) { | |
1237 | panic!("frameworks not supported") | |
1238 | } | |
1239 | ||
1240 | fn output_filename(&mut self, path: &Path) { | |
1241 | self.cmd.arg("-o").arg(path); | |
1242 | } | |
1243 | ||
1244 | fn add_object(&mut self, path: &Path) { | |
1245 | self.cmd.arg(path); | |
1246 | } | |
1247 | ||
dfeec247 | 1248 | fn full_relro(&mut self) {} |
0531ce1d | 1249 | |
dfeec247 | 1250 | fn partial_relro(&mut self) {} |
0531ce1d | 1251 | |
dfeec247 | 1252 | fn no_relro(&mut self) {} |
0531ce1d | 1253 | |
064997fb FG |
1254 | fn link_rust_dylib(&mut self, lib: &str, _path: &Path) { |
1255 | self.cmd.arg("-l").arg(lib); | |
0531ce1d XL |
1256 | } |
1257 | ||
064997fb | 1258 | fn link_framework(&mut self, _framework: &str, _as_needed: bool) { |
0531ce1d XL |
1259 | panic!("frameworks not supported") |
1260 | } | |
1261 | ||
064997fb | 1262 | fn link_whole_staticlib(&mut self, lib: &str, _verbatim: bool, _search_path: &[PathBuf]) { |
487cf647 | 1263 | self.cmd.arg("--whole-archive").arg("-l").arg(lib).arg("--no-whole-archive"); |
0531ce1d XL |
1264 | } |
1265 | ||
1266 | fn link_whole_rlib(&mut self, lib: &Path) { | |
487cf647 | 1267 | self.cmd.arg("--whole-archive").arg(lib).arg("--no-whole-archive"); |
0531ce1d XL |
1268 | } |
1269 | ||
1270 | fn gc_sections(&mut self, _keep_metadata: bool) { | |
8faf50e0 | 1271 | self.cmd.arg("--gc-sections"); |
0531ce1d XL |
1272 | } |
1273 | ||
17df50a5 XL |
1274 | fn no_gc_sections(&mut self) { |
1275 | self.cmd.arg("--no-gc-sections"); | |
1276 | } | |
1277 | ||
0531ce1d | 1278 | fn optimize(&mut self) { |
8faf50e0 XL |
1279 | self.cmd.arg(match self.sess.opts.optimize { |
1280 | OptLevel::No => "-O0", | |
1281 | OptLevel::Less => "-O1", | |
1282 | OptLevel::Default => "-O2", | |
1283 | OptLevel::Aggressive => "-O3", | |
1284 | // Currently LLD doesn't support `Os` and `Oz`, so pass through `O2` | |
1285 | // instead. | |
1286 | OptLevel::Size => "-O2", | |
dfeec247 | 1287 | OptLevel::SizeMin => "-O2", |
8faf50e0 | 1288 | }); |
0531ce1d XL |
1289 | } |
1290 | ||
dfeec247 | 1291 | fn pgo_gen(&mut self) {} |
0531ce1d | 1292 | |
04454e1e | 1293 | fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) { |
f9f354fc XL |
1294 | match strip { |
1295 | Strip::None => {} | |
1296 | Strip::Debuginfo => { | |
1297 | self.cmd.arg("--strip-debug"); | |
1298 | } | |
1299 | Strip::Symbols => { | |
1300 | self.cmd.arg("--strip-all"); | |
1301 | } | |
1302 | } | |
1303 | } | |
0531ce1d | 1304 | |
f035d41b | 1305 | fn control_flow_guard(&mut self) {} |
74b04a01 | 1306 | |
f9f354fc | 1307 | fn no_crt_objects(&mut self) {} |
0531ce1d | 1308 | |
f9f354fc | 1309 | fn no_default_libraries(&mut self) {} |
0531ce1d | 1310 | |
136023e0 XL |
1311 | fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { |
1312 | for sym in symbols { | |
0bf4aa26 XL |
1313 | self.cmd.arg("--export").arg(&sym); |
1314 | } | |
416331ca | 1315 | |
f035d41b | 1316 | // LLD will hide these otherwise-internal symbols since it only exports |
5e7ed085 | 1317 | // symbols explicitly passed via the `--export` flags above and hides all |
2b03887a FG |
1318 | // others. Various bits and pieces of wasm32-unknown-unknown tooling use |
1319 | // this, so be sure these symbols make their way out of the linker as well. | |
1320 | if self.sess.target.os == "unknown" { | |
1321 | self.cmd.arg("--export=__heap_base"); | |
1322 | self.cmd.arg("--export=__data_end"); | |
1323 | } | |
0531ce1d XL |
1324 | } |
1325 | ||
dfeec247 | 1326 | fn subsystem(&mut self, _subsystem: &str) {} |
0531ce1d | 1327 | |
9fa01778 | 1328 | fn linker_plugin_lto(&mut self) { |
94b46f34 XL |
1329 | // Do nothing for now |
1330 | } | |
0531ce1d | 1331 | } |
a1dfa0c6 | 1332 | |
5099ac24 FG |
1333 | /// Linker shepherd script for L4Re (Fiasco) |
1334 | pub struct L4Bender<'a> { | |
1335 | cmd: Command, | |
1336 | sess: &'a Session, | |
1337 | hinted_static: bool, | |
1338 | } | |
1339 | ||
1340 | impl<'a> Linker for L4Bender<'a> { | |
064997fb | 1341 | fn link_dylib(&mut self, _lib: &str, _verbatim: bool, _as_needed: bool) { |
5099ac24 FG |
1342 | bug!("dylibs are not supported on L4Re"); |
1343 | } | |
064997fb | 1344 | fn link_staticlib(&mut self, lib: &str, _verbatim: bool) { |
5099ac24 FG |
1345 | self.hint_static(); |
1346 | self.cmd.arg(format!("-PC{}", lib)); | |
1347 | } | |
1348 | fn link_rlib(&mut self, lib: &Path) { | |
1349 | self.hint_static(); | |
1350 | self.cmd.arg(lib); | |
1351 | } | |
1352 | fn include_path(&mut self, path: &Path) { | |
1353 | self.cmd.arg("-L").arg(path); | |
1354 | } | |
1355 | fn framework_path(&mut self, _: &Path) { | |
1356 | bug!("frameworks are not supported on L4Re"); | |
1357 | } | |
1358 | fn output_filename(&mut self, path: &Path) { | |
1359 | self.cmd.arg("-o").arg(path); | |
1360 | } | |
1361 | ||
1362 | fn add_object(&mut self, path: &Path) { | |
1363 | self.cmd.arg(path); | |
1364 | } | |
1365 | ||
1366 | fn full_relro(&mut self) { | |
9ffffee4 FG |
1367 | self.cmd.arg("-z").arg("relro"); |
1368 | self.cmd.arg("-z").arg("now"); | |
5099ac24 FG |
1369 | } |
1370 | ||
1371 | fn partial_relro(&mut self) { | |
9ffffee4 | 1372 | self.cmd.arg("-z").arg("relro"); |
5099ac24 FG |
1373 | } |
1374 | ||
1375 | fn no_relro(&mut self) { | |
9ffffee4 | 1376 | self.cmd.arg("-z").arg("norelro"); |
5099ac24 FG |
1377 | } |
1378 | ||
1379 | fn cmd(&mut self) -> &mut Command { | |
1380 | &mut self.cmd | |
1381 | } | |
1382 | ||
1383 | fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {} | |
1384 | ||
064997fb | 1385 | fn link_rust_dylib(&mut self, _: &str, _: &Path) { |
5099ac24 FG |
1386 | panic!("Rust dylibs not supported"); |
1387 | } | |
1388 | ||
064997fb | 1389 | fn link_framework(&mut self, _framework: &str, _as_needed: bool) { |
5099ac24 FG |
1390 | bug!("frameworks not supported on L4Re"); |
1391 | } | |
1392 | ||
064997fb | 1393 | fn link_whole_staticlib(&mut self, lib: &str, _verbatim: bool, _search_path: &[PathBuf]) { |
5099ac24 FG |
1394 | self.hint_static(); |
1395 | self.cmd.arg("--whole-archive").arg(format!("-l{}", lib)); | |
1396 | self.cmd.arg("--no-whole-archive"); | |
1397 | } | |
1398 | ||
1399 | fn link_whole_rlib(&mut self, lib: &Path) { | |
1400 | self.hint_static(); | |
1401 | self.cmd.arg("--whole-archive").arg(lib).arg("--no-whole-archive"); | |
1402 | } | |
1403 | ||
1404 | fn gc_sections(&mut self, keep_metadata: bool) { | |
1405 | if !keep_metadata { | |
1406 | self.cmd.arg("--gc-sections"); | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | fn no_gc_sections(&mut self) { | |
1411 | self.cmd.arg("--no-gc-sections"); | |
1412 | } | |
1413 | ||
1414 | fn optimize(&mut self) { | |
1415 | // GNU-style linkers support optimization with -O. GNU ld doesn't | |
1416 | // need a numeric argument, but other linkers do. | |
1417 | if self.sess.opts.optimize == config::OptLevel::Default | |
1418 | || self.sess.opts.optimize == config::OptLevel::Aggressive | |
1419 | { | |
1420 | self.cmd.arg("-O1"); | |
1421 | } | |
1422 | } | |
1423 | ||
1424 | fn pgo_gen(&mut self) {} | |
1425 | ||
04454e1e | 1426 | fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) { |
5099ac24 FG |
1427 | match strip { |
1428 | Strip::None => {} | |
1429 | Strip::Debuginfo => { | |
1430 | self.cmd().arg("--strip-debug"); | |
1431 | } | |
1432 | Strip::Symbols => { | |
1433 | self.cmd().arg("--strip-all"); | |
1434 | } | |
1435 | } | |
1436 | } | |
1437 | ||
1438 | fn no_default_libraries(&mut self) { | |
1439 | self.cmd.arg("-nostdlib"); | |
1440 | } | |
1441 | ||
1442 | fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[String]) { | |
1443 | // ToDo, not implemented, copy from GCC | |
2b03887a | 1444 | self.sess.emit_warning(errors::L4BenderExportingSymbolsUnimplemented); |
5099ac24 FG |
1445 | return; |
1446 | } | |
1447 | ||
1448 | fn subsystem(&mut self, subsystem: &str) { | |
1449 | self.cmd.arg(&format!("--subsystem {}", subsystem)); | |
1450 | } | |
1451 | ||
1452 | fn reset_per_library_state(&mut self) { | |
1453 | self.hint_static(); // Reset to default before returning the composed command line. | |
1454 | } | |
1455 | ||
5099ac24 FG |
1456 | fn linker_plugin_lto(&mut self) {} |
1457 | ||
1458 | fn control_flow_guard(&mut self) {} | |
1459 | ||
1460 | fn no_crt_objects(&mut self) {} | |
1461 | } | |
1462 | ||
1463 | impl<'a> L4Bender<'a> { | |
1464 | pub fn new(cmd: Command, sess: &'a Session) -> L4Bender<'a> { | |
1465 | L4Bender { cmd: cmd, sess: sess, hinted_static: false } | |
1466 | } | |
1467 | ||
1468 | fn hint_static(&mut self) { | |
1469 | if !self.hinted_static { | |
1470 | self.cmd.arg("-static"); | |
1471 | self.hinted_static = true; | |
1472 | } | |
1473 | } | |
1474 | } | |
1475 | ||
04454e1e FG |
1476 | fn for_each_exported_symbols_include_dep<'tcx>( |
1477 | tcx: TyCtxt<'tcx>, | |
1478 | crate_type: CrateType, | |
1479 | mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum), | |
1480 | ) { | |
1481 | for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() { | |
1482 | callback(symbol, info, LOCAL_CRATE); | |
1483 | } | |
1484 | ||
1485 | let formats = tcx.dependency_formats(()); | |
1486 | let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap(); | |
1487 | ||
1488 | for (index, dep_format) in deps.iter().enumerate() { | |
1489 | let cnum = CrateNum::new(index + 1); | |
1490 | // For each dependency that we are linking to statically ... | |
1491 | if *dep_format == Linkage::Static { | |
1492 | for &(symbol, info) in tcx.exported_symbols(cnum).iter() { | |
1493 | callback(symbol, info, cnum); | |
1494 | } | |
1495 | } | |
1496 | } | |
1497 | } | |
1498 | ||
136023e0 | 1499 | pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> { |
29967ef6 | 1500 | if let Some(ref exports) = tcx.sess.target.override_export_symbols { |
5e7ed085 | 1501 | return exports.iter().map(ToString::to_string).collect(); |
a1dfa0c6 XL |
1502 | } |
1503 | ||
1504 | let mut symbols = Vec::new(); | |
1505 | ||
1506 | let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); | |
04454e1e FG |
1507 | for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| { |
1508 | if info.level.is_below_threshold(export_threshold) { | |
1509 | symbols.push(symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum)); | |
a1dfa0c6 | 1510 | } |
04454e1e | 1511 | }); |
a1dfa0c6 | 1512 | |
04454e1e FG |
1513 | symbols |
1514 | } | |
e74abb32 | 1515 | |
04454e1e FG |
1516 | pub(crate) fn linked_symbols( |
1517 | tcx: TyCtxt<'_>, | |
1518 | crate_type: CrateType, | |
1519 | ) -> Vec<(String, SymbolExportKind)> { | |
1520 | match crate_type { | |
1521 | CrateType::Executable | CrateType::Cdylib | CrateType::Dylib => (), | |
1522 | CrateType::Staticlib | CrateType::ProcMacro | CrateType::Rlib => { | |
1523 | return Vec::new(); | |
a1dfa0c6 XL |
1524 | } |
1525 | } | |
1526 | ||
04454e1e FG |
1527 | let mut symbols = Vec::new(); |
1528 | ||
1529 | let export_threshold = symbol_export::crates_export_threshold(&[crate_type]); | |
1530 | for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| { | |
1531 | if info.level.is_below_threshold(export_threshold) || info.used { | |
1532 | symbols.push(( | |
1533 | symbol_export::linking_symbol_name_for_instance_in_crate(tcx, symbol, cnum), | |
1534 | info.kind, | |
1535 | )); | |
1536 | } | |
1537 | }); | |
1538 | ||
a1dfa0c6 XL |
1539 | symbols |
1540 | } | |
9fa01778 XL |
1541 | |
1542 | /// Much simplified and explicit CLI for the NVPTX linker. The linker operates | |
1543 | /// with bitcode and uses LLVM backend to generate a PTX assembly. | |
1544 | pub struct PtxLinker<'a> { | |
1545 | cmd: Command, | |
1546 | sess: &'a Session, | |
1547 | } | |
1548 | ||
1549 | impl<'a> Linker for PtxLinker<'a> { | |
ba9703b0 XL |
1550 | fn cmd(&mut self) -> &mut Command { |
1551 | &mut self.cmd | |
1552 | } | |
1553 | ||
f9f354fc XL |
1554 | fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {} |
1555 | ||
9fa01778 XL |
1556 | fn link_rlib(&mut self, path: &Path) { |
1557 | self.cmd.arg("--rlib").arg(path); | |
1558 | } | |
1559 | ||
1560 | fn link_whole_rlib(&mut self, path: &Path) { | |
1561 | self.cmd.arg("--rlib").arg(path); | |
1562 | } | |
1563 | ||
1564 | fn include_path(&mut self, path: &Path) { | |
1565 | self.cmd.arg("-L").arg(path); | |
1566 | } | |
1567 | ||
04454e1e | 1568 | fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) { |
9fa01778 XL |
1569 | self.cmd.arg("--debug"); |
1570 | } | |
1571 | ||
1572 | fn add_object(&mut self, path: &Path) { | |
1573 | self.cmd.arg("--bitcode").arg(path); | |
1574 | } | |
1575 | ||
9fa01778 XL |
1576 | fn optimize(&mut self) { |
1577 | match self.sess.lto() { | |
1578 | Lto::Thin | Lto::Fat | Lto::ThinLocal => { | |
1579 | self.cmd.arg("-Olto"); | |
dfeec247 | 1580 | } |
9fa01778 | 1581 | |
dfeec247 | 1582 | Lto::No => {} |
9fa01778 XL |
1583 | }; |
1584 | } | |
1585 | ||
1586 | fn output_filename(&mut self, path: &Path) { | |
1587 | self.cmd.arg("-o").arg(path); | |
1588 | } | |
1589 | ||
064997fb | 1590 | fn link_dylib(&mut self, _lib: &str, _verbatim: bool, _as_needed: bool) { |
17df50a5 XL |
1591 | panic!("external dylibs not supported") |
1592 | } | |
1593 | ||
064997fb | 1594 | fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) { |
17df50a5 XL |
1595 | panic!("external dylibs not supported") |
1596 | } | |
1597 | ||
064997fb | 1598 | fn link_staticlib(&mut self, _lib: &str, _verbatim: bool) { |
17df50a5 XL |
1599 | panic!("staticlibs not supported") |
1600 | } | |
1601 | ||
064997fb | 1602 | fn link_whole_staticlib(&mut self, _lib: &str, _verbatim: bool, _search_path: &[PathBuf]) { |
17df50a5 XL |
1603 | panic!("staticlibs not supported") |
1604 | } | |
1605 | ||
1606 | fn framework_path(&mut self, _path: &Path) { | |
1607 | panic!("frameworks not supported") | |
1608 | } | |
1609 | ||
064997fb | 1610 | fn link_framework(&mut self, _framework: &str, _as_needed: bool) { |
17df50a5 XL |
1611 | panic!("frameworks not supported") |
1612 | } | |
1613 | ||
1614 | fn full_relro(&mut self) {} | |
1615 | ||
1616 | fn partial_relro(&mut self) {} | |
1617 | ||
1618 | fn no_relro(&mut self) {} | |
1619 | ||
1620 | fn gc_sections(&mut self, _keep_metadata: bool) {} | |
1621 | ||
1622 | fn no_gc_sections(&mut self) {} | |
1623 | ||
1624 | fn pgo_gen(&mut self) {} | |
1625 | ||
1626 | fn no_crt_objects(&mut self) {} | |
1627 | ||
1628 | fn no_default_libraries(&mut self) {} | |
1629 | ||
1630 | fn control_flow_guard(&mut self) {} | |
1631 | ||
136023e0 | 1632 | fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, _symbols: &[String]) {} |
17df50a5 XL |
1633 | |
1634 | fn subsystem(&mut self, _subsystem: &str) {} | |
1635 | ||
17df50a5 XL |
1636 | fn linker_plugin_lto(&mut self) {} |
1637 | } | |
1638 | ||
1639 | pub struct BpfLinker<'a> { | |
1640 | cmd: Command, | |
1641 | sess: &'a Session, | |
17df50a5 XL |
1642 | } |
1643 | ||
1644 | impl<'a> Linker for BpfLinker<'a> { | |
1645 | fn cmd(&mut self) -> &mut Command { | |
1646 | &mut self.cmd | |
1647 | } | |
1648 | ||
1649 | fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {} | |
1650 | ||
1651 | fn link_rlib(&mut self, path: &Path) { | |
1652 | self.cmd.arg(path); | |
1653 | } | |
1654 | ||
1655 | fn link_whole_rlib(&mut self, path: &Path) { | |
1656 | self.cmd.arg(path); | |
1657 | } | |
1658 | ||
1659 | fn include_path(&mut self, path: &Path) { | |
1660 | self.cmd.arg("-L").arg(path); | |
1661 | } | |
1662 | ||
04454e1e | 1663 | fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) { |
17df50a5 XL |
1664 | self.cmd.arg("--debug"); |
1665 | } | |
1666 | ||
1667 | fn add_object(&mut self, path: &Path) { | |
1668 | self.cmd.arg(path); | |
1669 | } | |
1670 | ||
1671 | fn optimize(&mut self) { | |
1672 | self.cmd.arg(match self.sess.opts.optimize { | |
1673 | OptLevel::No => "-O0", | |
1674 | OptLevel::Less => "-O1", | |
1675 | OptLevel::Default => "-O2", | |
1676 | OptLevel::Aggressive => "-O3", | |
1677 | OptLevel::Size => "-Os", | |
1678 | OptLevel::SizeMin => "-Oz", | |
9fa01778 | 1679 | }); |
9fa01778 XL |
1680 | } |
1681 | ||
17df50a5 XL |
1682 | fn output_filename(&mut self, path: &Path) { |
1683 | self.cmd.arg("-o").arg(path); | |
1684 | } | |
1685 | ||
064997fb | 1686 | fn link_dylib(&mut self, _lib: &str, _verbatim: bool, _as_needed: bool) { |
9fa01778 XL |
1687 | panic!("external dylibs not supported") |
1688 | } | |
1689 | ||
064997fb | 1690 | fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) { |
9fa01778 XL |
1691 | panic!("external dylibs not supported") |
1692 | } | |
1693 | ||
064997fb | 1694 | fn link_staticlib(&mut self, _lib: &str, _verbatim: bool) { |
9fa01778 XL |
1695 | panic!("staticlibs not supported") |
1696 | } | |
1697 | ||
064997fb | 1698 | fn link_whole_staticlib(&mut self, _lib: &str, _verbatim: bool, _search_path: &[PathBuf]) { |
9fa01778 XL |
1699 | panic!("staticlibs not supported") |
1700 | } | |
1701 | ||
1702 | fn framework_path(&mut self, _path: &Path) { | |
1703 | panic!("frameworks not supported") | |
1704 | } | |
1705 | ||
064997fb | 1706 | fn link_framework(&mut self, _framework: &str, _as_needed: bool) { |
9fa01778 XL |
1707 | panic!("frameworks not supported") |
1708 | } | |
1709 | ||
dfeec247 | 1710 | fn full_relro(&mut self) {} |
9fa01778 | 1711 | |
dfeec247 | 1712 | fn partial_relro(&mut self) {} |
9fa01778 | 1713 | |
dfeec247 | 1714 | fn no_relro(&mut self) {} |
9fa01778 | 1715 | |
dfeec247 | 1716 | fn gc_sections(&mut self, _keep_metadata: bool) {} |
9fa01778 | 1717 | |
17df50a5 XL |
1718 | fn no_gc_sections(&mut self) {} |
1719 | ||
dfeec247 | 1720 | fn pgo_gen(&mut self) {} |
9fa01778 | 1721 | |
f9f354fc XL |
1722 | fn no_crt_objects(&mut self) {} |
1723 | ||
dfeec247 | 1724 | fn no_default_libraries(&mut self) {} |
9fa01778 | 1725 | |
f035d41b | 1726 | fn control_flow_guard(&mut self) {} |
74b04a01 | 1727 | |
136023e0 | 1728 | fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { |
17df50a5 XL |
1729 | let path = tmpdir.join("symbols"); |
1730 | let res: io::Result<()> = try { | |
1731 | let mut f = BufWriter::new(File::create(&path)?); | |
136023e0 | 1732 | for sym in symbols { |
17df50a5 XL |
1733 | writeln!(f, "{}", sym)?; |
1734 | } | |
1735 | }; | |
2b03887a FG |
1736 | if let Err(error) = res { |
1737 | self.sess.emit_fatal(errors::SymbolFileWriteFailure { error }); | |
17df50a5 XL |
1738 | } else { |
1739 | self.cmd.arg("--export-symbols").arg(&path); | |
1740 | } | |
1741 | } | |
9fa01778 | 1742 | |
dfeec247 | 1743 | fn subsystem(&mut self, _subsystem: &str) {} |
9fa01778 | 1744 | |
dfeec247 | 1745 | fn linker_plugin_lto(&mut self) {} |
9fa01778 | 1746 | } |