-Z linker-flavor
This patch adds a `-Z linker-flavor` flag to rustc which can be used to invoke
the linker using a different interface.
For example, by default rustc assumes that all the Linux targets will be linked
using GCC. This makes it impossible to use LLD as a linker using just `-C
linker=ld.lld` because that will invoke LLD with invalid command line
arguments. (e.g. rustc will pass -Wl,--gc-sections to LLD but LLD doesn't
understand that; --gc-sections would be the right argument)
With this patch one can pass `-Z linker-flavor=ld` to rustc to invoke the linker
using a LD-like interface. This way, `rustc -C linker=ld.lld -Z
linker-flavor=ld` will invoke LLD with the right arguments.
`-Z linker-flavor` accepts 4 different arguments: `em` (emcc), `ld`,
`gcc`, `msvc` (link.exe). `em`, `gnu` and `msvc` cover all the existing linker
interfaces. `ld` is a new flavor for interfacing GNU's ld and LLD.
This patch also changes target specifications. `linker-flavor` is now a
mandatory field that specifies the *default* linker flavor that the target will
use. This change also makes the linker interface *explicit*; before, it used to
be derived from other fields like linker-is-gnu, is-like-msvc,
is-like-emscripten, etc.
Another change to target specifications is that the fields `pre-link-args`,
`post-link-args` and `late-link-args` now expect a map from flavor to linker
arguments.
``` diff
- "pre-link-args": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "pre-link-args": {
+ "gcc": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "ld": ["--as-needed", "-z,-noexecstack"],
+ },
```
[breaking-change] for users of custom targets specifications
2017-02-21 14:47:15 -05:00
|
|
|
use std::ffi::{OsStr, OsString};
|
2015-07-28 17:19:08 -07:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::prelude::*;
|
rustc: Implement custom derive (macros 1.1)
This commit is an implementation of [RFC 1681] which adds support to the
compiler for first-class user-define custom `#[derive]` modes with a far more
stable API than plugins have today.
[RFC 1681]: https://github.com/rust-lang/rfcs/blob/master/text/1681-macros-1.1.md
The main features added by this commit are:
* A new `rustc-macro` crate-type. This crate type represents one which will
provide custom `derive` implementations and perhaps eventually flower into the
implementation of macros 2.0 as well.
* A new `rustc_macro` crate in the standard distribution. This crate will
provide the runtime interface between macro crates and the compiler. The API
here is particularly conservative right now but has quite a bit of room to
expand into any manner of APIs required by macro authors.
* The ability to load new derive modes through the `#[macro_use]` annotations on
other crates.
All support added here is gated behind the `rustc_macro` feature gate, both for
the library support (the `rustc_macro` crate) as well as the language features.
There are a few minor differences from the implementation outlined in the RFC,
such as the `rustc_macro` crate being available as a dylib and all symbols are
`dlsym`'d directly instead of having a shim compiled. These should only affect
the implementation, however, not the public interface.
This commit also ended up touching a lot of code related to `#[derive]`, making
a few notable changes:
* Recognized derive attributes are no longer desugared to `derive_Foo`. Wasn't
sure how to keep this behavior and *not* expose it to custom derive.
* Derive attributes no longer have access to unstable features by default, they
have to opt in on a granular level.
* The `derive(Copy,Clone)` optimization is now done through another "obscure
attribute" which is just intended to ferry along in the compiler that such an
optimization is possible. The `derive(PartialEq,Eq)` optimization was also
updated to do something similar.
---
One part of this PR which needs to be improved before stabilizing are the errors
and exact interfaces here. The error messages are relatively poor quality and
there are surprising spects of this such as `#[derive(PartialEq, Eq, MyTrait)]`
not working by default. The custom attributes added by the compiler end up
becoming unstable again when going through a custom impl.
Hopefully though this is enough to start allowing experimentation on crates.io!
syntax-[breaking-change]
2016-08-22 17:07:11 -07:00
|
|
|
use std::io::{self, BufWriter};
|
2015-05-08 15:31:23 -07:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-01-27 19:35:55 +03:00
|
|
|
use std::{env, iter, mem, str};
|
2015-05-08 15:31:23 -07:00
|
|
|
|
2021-07-06 17:52:11 +02:00
|
|
|
use cc::windows_registry;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
|
2022-08-24 13:10:40 +03:00
|
|
|
use rustc_metadata::find_native_static_library;
|
2024-04-29 13:56:41 +10:00
|
|
|
use rustc_middle::bug;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::dependency_format::Linkage;
|
2023-07-21 09:41:07 -04:00
|
|
|
use rustc_middle::middle::exported_symbols;
|
2022-04-10 02:16:12 +01:00
|
|
|
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo, SymbolExportKind};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-05-03 12:36:12 +08:00
|
|
|
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
|
2020-03-11 12:49:08 +01:00
|
|
|
use rustc_session::Session;
|
2020-06-23 09:41:56 -07:00
|
|
|
use rustc_span::symbol::sym;
|
2022-08-06 21:08:46 +03:00
|
|
|
use rustc_target::spec::{Cc, LinkOutputKind, LinkerFlavor, Lld};
|
2024-05-22 15:08:26 +10:00
|
|
|
use tracing::{debug, warn};
|
2021-07-06 17:52:11 +02:00
|
|
|
|
2018-10-23 17:01:35 +02:00
|
|
|
use super::command::Command;
|
|
|
|
use super::symbol_export;
|
2022-10-07 10:03:45 -04:00
|
|
|
use crate::errors;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2020-04-27 23:37:57 +03:00
|
|
|
/// Disables non-English messages from localized linkers.
|
|
|
|
/// Such messages may cause issues with text encoding on Windows (#35785)
|
|
|
|
/// and prevent inspection of linker output in case of errors, which we occasionally do.
|
|
|
|
/// This should be acceptable because other messages from rustc are in English anyway,
|
|
|
|
/// and may also be desirable to improve searchability of the linker diagnostics.
|
|
|
|
pub fn disable_localization(linker: &mut Command) {
|
|
|
|
// No harm in setting both env vars simultaneously.
|
|
|
|
// Unix-style linkers.
|
2020-07-18 16:59:16 +02:00
|
|
|
linker.env("LC_ALL", "C");
|
2020-04-27 23:37:57 +03:00
|
|
|
// MSVC's `link.exe`.
|
|
|
|
linker.env("VSLANG", "1033");
|
|
|
|
}
|
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// The third parameter is for env vars, used on windows to set up the
|
|
|
|
/// path for MSVC to find its DLLs, and gcc to find its bundled
|
|
|
|
/// toolchain
|
2021-07-06 18:01:44 +02:00
|
|
|
pub fn get_linker<'a>(
|
|
|
|
sess: &'a Session,
|
2021-07-06 17:52:11 +02:00
|
|
|
linker: &Path,
|
|
|
|
flavor: LinkerFlavor,
|
|
|
|
self_contained: bool,
|
2021-07-06 18:28:07 +02:00
|
|
|
target_cpu: &'a str,
|
2021-07-06 18:01:44 +02:00
|
|
|
) -> Box<dyn Linker + 'a> {
|
2023-11-21 20:07:32 +01:00
|
|
|
let msvc_tool = windows_registry::find_tool(sess.opts.target_triple.triple(), "link.exe");
|
2021-07-06 17:52:11 +02:00
|
|
|
|
|
|
|
// If our linker looks like a batch script on Windows then to execute this
|
|
|
|
// we'll need to spawn `cmd` explicitly. This is primarily done to handle
|
|
|
|
// emscripten where the linker is `emcc.bat` and needs to be spawned as
|
|
|
|
// `cmd /c emcc.bat ...`.
|
|
|
|
//
|
|
|
|
// This worked historically but is needed manually since #42436 (regression
|
|
|
|
// was tagged as #42791) and some more info can be found on #44443 for
|
|
|
|
// emscripten itself.
|
|
|
|
let mut cmd = match linker.to_str() {
|
|
|
|
Some(linker) if cfg!(windows) && linker.ends_with(".bat") => Command::bat_script(linker),
|
|
|
|
_ => match flavor {
|
2022-08-06 21:08:46 +03:00
|
|
|
LinkerFlavor::Gnu(Cc::No, Lld::Yes)
|
|
|
|
| LinkerFlavor::Darwin(Cc::No, Lld::Yes)
|
|
|
|
| LinkerFlavor::WasmLld(Cc::No)
|
|
|
|
| LinkerFlavor::Msvc(Lld::Yes) => Command::lld(linker, flavor.lld_flavor()),
|
|
|
|
LinkerFlavor::Msvc(Lld::No)
|
|
|
|
if sess.opts.cg.linker.is_none() && sess.target.linker.is_none() =>
|
|
|
|
{
|
2021-07-06 17:52:11 +02:00
|
|
|
Command::new(msvc_tool.as_ref().map_or(linker, |t| t.path()))
|
|
|
|
}
|
|
|
|
_ => Command::new(linker),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// UWP apps have API restrictions enforced during Store submissions.
|
|
|
|
// To comply with the Windows App Certification Kit,
|
|
|
|
// MSVC needs to link with the Store versions of the runtime libraries (vcruntime, msvcrt, etc).
|
|
|
|
let t = &sess.target;
|
2022-08-06 21:08:46 +03:00
|
|
|
if matches!(flavor, LinkerFlavor::Msvc(..)) && t.vendor == "uwp" {
|
2021-07-06 17:52:11 +02:00
|
|
|
if let Some(ref tool) = msvc_tool {
|
|
|
|
let original_path = tool.path();
|
2023-11-21 20:07:32 +01:00
|
|
|
if let Some(root_lib_path) = original_path.ancestors().nth(4) {
|
2022-03-22 11:43:05 +01:00
|
|
|
let arch = match t.arch.as_ref() {
|
2021-07-06 17:52:11 +02:00
|
|
|
"x86_64" => Some("x64"),
|
|
|
|
"x86" => Some("x86"),
|
|
|
|
"aarch64" => Some("arm64"),
|
|
|
|
"arm" => Some("arm"),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(ref a) = arch {
|
|
|
|
// FIXME: Move this to `fn linker_with_args`.
|
|
|
|
let mut arg = OsString::from("/LIBPATH:");
|
|
|
|
arg.push(format!("{}\\lib\\{}\\store", root_lib_path.display(), a));
|
|
|
|
cmd.arg(&arg);
|
|
|
|
} else {
|
2022-10-04 13:25:13 -04:00
|
|
|
warn!("arch is not supported");
|
2021-07-06 17:52:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-04 13:25:13 -04:00
|
|
|
warn!("MSVC root path lib location not found");
|
2021-07-06 17:52:11 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-04 13:25:13 -04:00
|
|
|
warn!("link.exe not found");
|
2021-07-06 17:52:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The compiler's sysroot often has some bundled tools, so add it to the
|
|
|
|
// PATH for the child.
|
2021-08-31 18:36:25 +02:00
|
|
|
let mut new_path = sess.get_tools_search_paths(self_contained);
|
2021-07-06 17:52:11 +02:00
|
|
|
let mut msvc_changed_path = false;
|
|
|
|
if sess.target.is_like_msvc {
|
|
|
|
if let Some(ref tool) = msvc_tool {
|
|
|
|
cmd.args(tool.args());
|
2022-12-18 17:01:58 +01:00
|
|
|
for (k, v) in tool.env() {
|
2021-07-06 17:52:11 +02:00
|
|
|
if k == "PATH" {
|
|
|
|
new_path.extend(env::split_paths(v));
|
|
|
|
msvc_changed_path = true;
|
|
|
|
} else {
|
|
|
|
cmd.env(k, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !msvc_changed_path {
|
|
|
|
if let Some(path) = env::var_os("PATH") {
|
|
|
|
new_path.extend(env::split_paths(&path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd.env("PATH", env::join_paths(new_path).unwrap());
|
|
|
|
|
2021-07-06 18:01:44 +02:00
|
|
|
// FIXME: Move `/LIBPATH` addition for uwp targets from the linker construction
|
|
|
|
// to the linker args construction.
|
|
|
|
assert!(cmd.get_args().is_empty() || sess.target.vendor == "uwp");
|
|
|
|
match flavor {
|
2022-08-06 21:08:46 +03:00
|
|
|
LinkerFlavor::Unix(Cc::No) if sess.target.os == "l4re" => {
|
2022-08-15 00:31:31 +03:00
|
|
|
Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>
|
|
|
|
}
|
2023-03-23 15:00:46 +08:00
|
|
|
LinkerFlavor::Unix(Cc::No) if sess.target.os == "aix" => {
|
|
|
|
Box::new(AixLinker::new(cmd, sess)) as Box<dyn Linker>
|
|
|
|
}
|
2022-08-06 21:08:46 +03:00
|
|
|
LinkerFlavor::WasmLld(Cc::No) => Box::new(WasmLd::new(cmd, sess)) as Box<dyn Linker>,
|
|
|
|
LinkerFlavor::Gnu(cc, _)
|
|
|
|
| LinkerFlavor::Darwin(cc, _)
|
|
|
|
| LinkerFlavor::WasmLld(cc)
|
|
|
|
| LinkerFlavor::Unix(cc) => Box::new(GccLinker {
|
|
|
|
cmd,
|
|
|
|
sess,
|
|
|
|
target_cpu,
|
2023-05-17 22:52:54 +02:00
|
|
|
hinted_static: None,
|
2022-08-06 21:08:46 +03:00
|
|
|
is_ld: cc == Cc::No,
|
|
|
|
is_gnu: flavor.is_gnu(),
|
|
|
|
}) as Box<dyn Linker>,
|
|
|
|
LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>,
|
2022-08-15 00:31:31 +03:00
|
|
|
LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
|
|
|
|
LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
|
2024-02-06 19:46:12 +01:00
|
|
|
LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box<dyn Linker>,
|
2022-08-15 00:31:31 +03:00
|
|
|
LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
|
2021-07-06 18:01:44 +02:00
|
|
|
}
|
2021-07-06 17:52:11 +02:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:35:55 +03:00
|
|
|
// Note: Ideally neither these helper function, nor the macro-generated inherent methods below
|
|
|
|
// would exist, and these functions would live in `trait Linker`.
|
|
|
|
// Unfortunately, adding these functions to `trait Linker` make it `dyn`-incompatible.
|
|
|
|
// If the methods are added to the trait with `where Self: Sized` bounds, then even a separate
|
|
|
|
// implementation of them for `dyn Linker {}` wouldn't work due to a conflict with those
|
|
|
|
// uncallable methods in the trait.
|
|
|
|
|
|
|
|
/// Just pass the arguments to the linker as is.
|
|
|
|
/// It is assumed that they are correctly prepared in advance.
|
|
|
|
fn verbatim_args<L: Linker + ?Sized>(
|
|
|
|
l: &mut L,
|
|
|
|
args: impl IntoIterator<Item: AsRef<OsStr>>,
|
|
|
|
) -> &mut L {
|
|
|
|
for arg in args {
|
|
|
|
l.cmd().arg(arg);
|
|
|
|
}
|
|
|
|
l
|
|
|
|
}
|
|
|
|
/// Arguments for the underlying linker.
|
|
|
|
/// Add options to pass them through cc wrapper if `Linker` is a cc wrapper.
|
|
|
|
fn link_args<L: Linker + ?Sized>(
|
|
|
|
l: &mut L,
|
|
|
|
args: impl IntoIterator<Item: AsRef<OsStr>, IntoIter: ExactSizeIterator>,
|
|
|
|
) -> &mut L {
|
|
|
|
let args = args.into_iter();
|
|
|
|
if !l.is_cc() {
|
|
|
|
verbatim_args(l, args);
|
|
|
|
} else if args.len() != 0 {
|
|
|
|
// FIXME: Support arguments with commas, see `rpaths_to_flags` for the example.
|
|
|
|
let mut combined_arg = OsString::from("-Wl");
|
|
|
|
for arg in args {
|
|
|
|
combined_arg.push(",");
|
|
|
|
combined_arg.push(arg);
|
|
|
|
}
|
|
|
|
l.cmd().arg(combined_arg);
|
|
|
|
}
|
|
|
|
l
|
|
|
|
}
|
|
|
|
/// Arguments for the cc wrapper specifically.
|
|
|
|
/// Check that it's indeed a cc wrapper and pass verbatim.
|
|
|
|
fn cc_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
|
|
|
|
assert!(l.is_cc());
|
|
|
|
verbatim_args(l, args)
|
|
|
|
}
|
|
|
|
/// Arguments supported by both underlying linker and cc wrapper, pass verbatim.
|
|
|
|
fn link_or_cc_args<L: Linker + ?Sized>(
|
|
|
|
l: &mut L,
|
|
|
|
args: impl IntoIterator<Item: AsRef<OsStr>>,
|
|
|
|
) -> &mut L {
|
|
|
|
verbatim_args(l, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! generate_arg_methods {
|
|
|
|
($($ty:ty)*) => { $(
|
|
|
|
impl $ty {
|
|
|
|
pub fn verbatim_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
|
|
|
|
verbatim_args(self, args)
|
|
|
|
}
|
|
|
|
pub fn verbatim_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
|
|
|
|
verbatim_args(self, iter::once(arg))
|
|
|
|
}
|
|
|
|
pub fn link_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>, IntoIter: ExactSizeIterator>) -> &mut Self {
|
|
|
|
link_args(self, args)
|
|
|
|
}
|
|
|
|
pub fn link_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
|
|
|
|
link_args(self, iter::once(arg))
|
|
|
|
}
|
|
|
|
pub fn cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
|
|
|
|
cc_args(self, args)
|
|
|
|
}
|
|
|
|
pub fn cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
|
|
|
|
cc_args(self, iter::once(arg))
|
|
|
|
}
|
|
|
|
pub fn link_or_cc_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
|
|
|
|
link_or_cc_args(self, args)
|
|
|
|
}
|
|
|
|
pub fn link_or_cc_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
|
|
|
|
link_or_cc_args(self, iter::once(arg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)* }
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_arg_methods! {
|
|
|
|
GccLinker<'_>
|
|
|
|
MsvcLinker<'_>
|
|
|
|
EmLinker<'_>
|
|
|
|
WasmLd<'_>
|
|
|
|
L4Bender<'_>
|
|
|
|
AixLinker<'_>
|
|
|
|
LlbcLinker<'_>
|
|
|
|
PtxLinker<'_>
|
|
|
|
BpfLinker<'_>
|
|
|
|
dyn Linker + '_
|
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Linker abstraction used by `back::link` to build up the command to invoke a
|
2015-05-08 15:31:23 -07:00
|
|
|
/// linker.
|
|
|
|
///
|
|
|
|
/// This trait is the total list of requirements needed by `back::link` and
|
|
|
|
/// represents the meaning of each option being passed down. This trait is then
|
|
|
|
/// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
|
2018-11-27 02:59:49 +00:00
|
|
|
/// MSVC linker (e.g., `link.exe`) is being used.
|
2015-05-08 15:31:23 -07:00
|
|
|
pub trait Linker {
|
2020-04-05 02:11:29 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command;
|
2024-01-27 19:35:55 +03:00
|
|
|
fn is_cc(&self) -> bool {
|
|
|
|
false
|
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
|
2024-06-06 19:57:36 +03:00
|
|
|
fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
|
|
|
|
bug!("dylib linked with unsupported linker")
|
|
|
|
}
|
|
|
|
fn link_dylib_by_path(&mut self, _path: &Path, _as_needed: bool) {
|
|
|
|
bug!("dylib linked with unsupported linker")
|
|
|
|
}
|
2024-01-17 23:14:14 +03:00
|
|
|
fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
|
|
|
|
bug!("framework linked with unsupported linker")
|
|
|
|
}
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool);
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
|
2024-01-27 19:35:55 +03:00
|
|
|
fn include_path(&mut self, path: &Path) {
|
|
|
|
link_or_cc_args(link_or_cc_args(self, &["-L"]), &[path]);
|
|
|
|
}
|
|
|
|
fn framework_path(&mut self, _path: &Path) {
|
|
|
|
bug!("framework path set with unsupported linker")
|
|
|
|
}
|
|
|
|
fn output_filename(&mut self, path: &Path) {
|
|
|
|
link_or_cc_args(link_or_cc_args(self, &["-o"]), &[path]);
|
|
|
|
}
|
|
|
|
fn add_object(&mut self, path: &Path) {
|
|
|
|
link_or_cc_args(self, &[path]);
|
|
|
|
}
|
2016-05-10 14:17:57 -07:00
|
|
|
fn gc_sections(&mut self, keep_metadata: bool);
|
2021-03-24 21:45:09 -07:00
|
|
|
fn no_gc_sections(&mut self);
|
2017-07-10 20:57:45 +02:00
|
|
|
fn full_relro(&mut self);
|
2018-02-22 21:28:20 +01:00
|
|
|
fn partial_relro(&mut self);
|
|
|
|
fn no_relro(&mut self);
|
2015-05-08 15:31:23 -07:00
|
|
|
fn optimize(&mut self);
|
2018-03-19 22:29:58 +01:00
|
|
|
fn pgo_gen(&mut self);
|
2020-01-13 13:25:39 +00:00
|
|
|
fn control_flow_guard(&mut self);
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self);
|
2022-05-24 11:14:48 -07:00
|
|
|
fn debuginfo(&mut self, strip: Strip, natvis_debugger_visualizers: &[PathBuf]);
|
2020-04-29 20:47:07 +03:00
|
|
|
fn no_crt_objects(&mut self);
|
2015-05-08 15:31:23 -07:00
|
|
|
fn no_default_libraries(&mut self);
|
2021-07-06 18:28:07 +02:00
|
|
|
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]);
|
2016-10-31 09:36:30 -07:00
|
|
|
fn subsystem(&mut self, subsystem: &str);
|
2019-02-01 15:15:43 +01:00
|
|
|
fn linker_plugin_lto(&mut self);
|
2020-06-20 23:26:24 +03:00
|
|
|
fn add_eh_frame_header(&mut self) {}
|
2021-03-28 23:18:39 +03:00
|
|
|
fn add_no_exec(&mut self) {}
|
2021-03-28 00:02:23 +03:00
|
|
|
fn add_as_needed(&mut self) {}
|
2021-05-08 20:38:13 +03:00
|
|
|
fn reset_per_library_state(&mut self) {}
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
|
2020-04-05 02:11:29 +03:00
|
|
|
impl dyn Linker + '_ {
|
2020-04-05 02:58:32 +03:00
|
|
|
pub fn take_cmd(&mut self) -> Command {
|
|
|
|
mem::replace(self.cmd(), Command::new(""))
|
|
|
|
}
|
2020-04-05 02:11:29 +03:00
|
|
|
}
|
|
|
|
|
-Z linker-flavor
This patch adds a `-Z linker-flavor` flag to rustc which can be used to invoke
the linker using a different interface.
For example, by default rustc assumes that all the Linux targets will be linked
using GCC. This makes it impossible to use LLD as a linker using just `-C
linker=ld.lld` because that will invoke LLD with invalid command line
arguments. (e.g. rustc will pass -Wl,--gc-sections to LLD but LLD doesn't
understand that; --gc-sections would be the right argument)
With this patch one can pass `-Z linker-flavor=ld` to rustc to invoke the linker
using a LD-like interface. This way, `rustc -C linker=ld.lld -Z
linker-flavor=ld` will invoke LLD with the right arguments.
`-Z linker-flavor` accepts 4 different arguments: `em` (emcc), `ld`,
`gcc`, `msvc` (link.exe). `em`, `gnu` and `msvc` cover all the existing linker
interfaces. `ld` is a new flavor for interfacing GNU's ld and LLD.
This patch also changes target specifications. `linker-flavor` is now a
mandatory field that specifies the *default* linker flavor that the target will
use. This change also makes the linker interface *explicit*; before, it used to
be derived from other fields like linker-is-gnu, is-like-msvc,
is-like-emscripten, etc.
Another change to target specifications is that the fields `pre-link-args`,
`post-link-args` and `late-link-args` now expect a map from flavor to linker
arguments.
``` diff
- "pre-link-args": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "pre-link-args": {
+ "gcc": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "ld": ["--as-needed", "-z,-noexecstack"],
+ },
```
[breaking-change] for users of custom targets specifications
2017-02-21 14:47:15 -05:00
|
|
|
pub struct GccLinker<'a> {
|
2017-03-23 18:03:39 -07:00
|
|
|
cmd: Command,
|
2016-05-25 01:45:25 +03:00
|
|
|
sess: &'a Session,
|
2021-07-06 18:09:49 +02:00
|
|
|
target_cpu: &'a str,
|
2023-05-17 22:52:54 +02:00
|
|
|
hinted_static: Option<bool>, // Keeps track of the current hinting mode.
|
-Z linker-flavor
This patch adds a `-Z linker-flavor` flag to rustc which can be used to invoke
the linker using a different interface.
For example, by default rustc assumes that all the Linux targets will be linked
using GCC. This makes it impossible to use LLD as a linker using just `-C
linker=ld.lld` because that will invoke LLD with invalid command line
arguments. (e.g. rustc will pass -Wl,--gc-sections to LLD but LLD doesn't
understand that; --gc-sections would be the right argument)
With this patch one can pass `-Z linker-flavor=ld` to rustc to invoke the linker
using a LD-like interface. This way, `rustc -C linker=ld.lld -Z
linker-flavor=ld` will invoke LLD with the right arguments.
`-Z linker-flavor` accepts 4 different arguments: `em` (emcc), `ld`,
`gcc`, `msvc` (link.exe). `em`, `gnu` and `msvc` cover all the existing linker
interfaces. `ld` is a new flavor for interfacing GNU's ld and LLD.
This patch also changes target specifications. `linker-flavor` is now a
mandatory field that specifies the *default* linker flavor that the target will
use. This change also makes the linker interface *explicit*; before, it used to
be derived from other fields like linker-is-gnu, is-like-msvc,
is-like-emscripten, etc.
Another change to target specifications is that the fields `pre-link-args`,
`post-link-args` and `late-link-args` now expect a map from flavor to linker
arguments.
``` diff
- "pre-link-args": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "pre-link-args": {
+ "gcc": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "ld": ["--as-needed", "-z,-noexecstack"],
+ },
```
[breaking-change] for users of custom targets specifications
2017-02-21 14:47:15 -05:00
|
|
|
// Link as ld
|
|
|
|
is_ld: bool,
|
2022-08-06 21:08:46 +03:00
|
|
|
is_gnu: bool,
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
|
-Z linker-flavor
This patch adds a `-Z linker-flavor` flag to rustc which can be used to invoke
the linker using a different interface.
For example, by default rustc assumes that all the Linux targets will be linked
using GCC. This makes it impossible to use LLD as a linker using just `-C
linker=ld.lld` because that will invoke LLD with invalid command line
arguments. (e.g. rustc will pass -Wl,--gc-sections to LLD but LLD doesn't
understand that; --gc-sections would be the right argument)
With this patch one can pass `-Z linker-flavor=ld` to rustc to invoke the linker
using a LD-like interface. This way, `rustc -C linker=ld.lld -Z
linker-flavor=ld` will invoke LLD with the right arguments.
`-Z linker-flavor` accepts 4 different arguments: `em` (emcc), `ld`,
`gcc`, `msvc` (link.exe). `em`, `gnu` and `msvc` cover all the existing linker
interfaces. `ld` is a new flavor for interfacing GNU's ld and LLD.
This patch also changes target specifications. `linker-flavor` is now a
mandatory field that specifies the *default* linker flavor that the target will
use. This change also makes the linker interface *explicit*; before, it used to
be derived from other fields like linker-is-gnu, is-like-msvc,
is-like-emscripten, etc.
Another change to target specifications is that the fields `pre-link-args`,
`post-link-args` and `late-link-args` now expect a map from flavor to linker
arguments.
``` diff
- "pre-link-args": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "pre-link-args": {
+ "gcc": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "ld": ["--as-needed", "-z,-noexecstack"],
+ },
```
[breaking-change] for users of custom targets specifications
2017-02-21 14:47:15 -05:00
|
|
|
impl<'a> GccLinker<'a> {
|
2015-05-08 15:31:23 -07:00
|
|
|
fn takes_hints(&self) -> bool {
|
2019-03-19 13:06:37 -07:00
|
|
|
// Really this function only returns true if the underlying linker
|
|
|
|
// configured for a compiler is binutils `ld.bfd` and `ld.gold`. We
|
|
|
|
// don't really have a foolproof way to detect that, so rule out some
|
|
|
|
// platforms where currently this is guaranteed to *not* be the case:
|
|
|
|
//
|
|
|
|
// * On OSX they have their own linker, not binutils'
|
|
|
|
// * For WebAssembly the only functional linker is LLD, which doesn't
|
|
|
|
// support hint flags
|
2020-12-30 12:52:21 -06:00
|
|
|
!self.sess.target.is_like_osx && !self.sess.target.is_like_wasm
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
2017-03-23 18:03:39 -07:00
|
|
|
|
|
|
|
// Some platforms take hints about whether a library is static or dynamic.
|
|
|
|
// For those that support this, we ensure we pass the option if the library
|
|
|
|
// was flagged "static" (most defaults are dynamic) to ensure that if
|
|
|
|
// libfoo.a and libfoo.so both exist that the right one is chosen.
|
|
|
|
fn hint_static(&mut self) {
|
|
|
|
if !self.takes_hints() {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 22:52:54 +02:00
|
|
|
if self.hinted_static != Some(true) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-Bstatic");
|
2023-05-17 22:52:54 +02:00
|
|
|
self.hinted_static = Some(true);
|
2017-03-23 18:03:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hint_dynamic(&mut self) {
|
|
|
|
if !self.takes_hints() {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 22:52:54 +02:00
|
|
|
if self.hinted_static != Some(false) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-Bdynamic");
|
2023-05-17 22:52:54 +02:00
|
|
|
self.hinted_static = Some(false);
|
2017-03-23 18:03:39 -07:00
|
|
|
}
|
|
|
|
}
|
2018-07-03 16:33:11 +02:00
|
|
|
|
2019-02-01 15:15:43 +01:00
|
|
|
fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
|
2018-07-03 16:33:11 +02:00
|
|
|
if let Some(plugin_path) = plugin_path {
|
|
|
|
let mut arg = OsString::from("-plugin=");
|
|
|
|
arg.push(plugin_path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&arg);
|
2018-07-03 16:33:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let opt_level = match self.sess.opts.optimize {
|
|
|
|
config::OptLevel::No => "O0",
|
|
|
|
config::OptLevel::Less => "O1",
|
2020-08-26 22:11:53 +02:00
|
|
|
config::OptLevel::Default | config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
|
2018-07-03 16:33:11 +02:00
|
|
|
config::OptLevel::Aggressive => "O3",
|
|
|
|
};
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&format!("-plugin-opt=sample-profile={}", path.display()));
|
2021-05-07 07:41:37 +00:00
|
|
|
};
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&[
|
2023-07-25 23:04:01 +02:00
|
|
|
&format!("-plugin-opt={opt_level}"),
|
2021-11-09 10:36:47 +11:00
|
|
|
&format!("-plugin-opt=mcpu={}", self.target_cpu),
|
|
|
|
]);
|
2018-07-03 16:33:11 +02:00
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
|
|
|
|
fn build_dylib(&mut self, out_filename: &Path) {
|
|
|
|
// On mac we need to tell the linker to let this library be rpathed
|
2020-11-08 14:27:51 +03:00
|
|
|
if self.sess.target.is_like_osx {
|
2021-11-09 10:44:49 +11:00
|
|
|
if !self.is_ld {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-dynamiclib");
|
2021-11-09 10:44:49 +11:00
|
|
|
}
|
|
|
|
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-dylib");
|
2020-05-02 02:22:48 +03:00
|
|
|
|
|
|
|
// Note that the `osx_rpath_install_name` option here is a hack
|
2024-07-07 00:07:08 +03:00
|
|
|
// purely to support bootstrap right now, we should get a more
|
2020-05-02 02:22:48 +03:00
|
|
|
// principled solution at some point to force the compiler to pass
|
|
|
|
// the right `-Wl,-install_name` with an `@rpath` in it.
|
2022-07-06 07:44:47 -05:00
|
|
|
if self.sess.opts.cg.rpath || self.sess.opts.unstable_opts.osx_rpath_install_name {
|
2021-11-09 10:36:47 +11:00
|
|
|
let mut rpath = OsString::from("@rpath/");
|
|
|
|
rpath.push(out_filename.file_name().unwrap());
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-install_name").link_arg(rpath);
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg("-shared");
|
2024-06-06 19:57:36 +03:00
|
|
|
if let Some(name) = out_filename.file_name() {
|
|
|
|
if self.sess.target.is_like_windows {
|
|
|
|
// The output filename already contains `dll_suffix` so
|
|
|
|
// the resulting import library will have a name in the
|
|
|
|
// form of libfoo.dll.a
|
|
|
|
let mut implib_name = OsString::from(&*self.sess.target.staticlib_prefix);
|
|
|
|
implib_name.push(name);
|
|
|
|
implib_name.push(&*self.sess.target.staticlib_suffix);
|
|
|
|
let mut out_implib = OsString::from("--out-implib=");
|
|
|
|
out_implib.push(out_filename.with_file_name(implib_name));
|
|
|
|
self.link_arg(out_implib);
|
|
|
|
} else {
|
|
|
|
// When dylibs are linked by a full path this value will get into `DT_NEEDED`
|
|
|
|
// instead of the full path, so the library can be later found in some other
|
|
|
|
// location than that specific path.
|
|
|
|
let mut soname = OsString::from("-soname=");
|
|
|
|
soname.push(name);
|
|
|
|
self.link_arg(soname);
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-06 19:57:36 +03:00
|
|
|
|
|
|
|
fn with_as_needed(&mut self, as_needed: bool, f: impl FnOnce(&mut Self)) {
|
|
|
|
if !as_needed {
|
|
|
|
if self.sess.target.is_like_osx {
|
|
|
|
// FIXME(81490): ld64 doesn't support these flags but macOS 11
|
|
|
|
// has -needed-l{} / -needed_library {}
|
|
|
|
// but we have no way to detect that here.
|
|
|
|
self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
|
|
|
|
} else if self.is_gnu && !self.sess.target.is_like_windows {
|
|
|
|
self.link_arg("--no-as-needed");
|
|
|
|
} else {
|
|
|
|
self.sess.dcx().emit_warn(errors::LinkerUnsupportedModifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f(self);
|
|
|
|
|
|
|
|
if !as_needed {
|
|
|
|
if self.sess.target.is_like_osx {
|
|
|
|
// See above FIXME comment
|
|
|
|
} else if self.is_gnu && !self.sess.target.is_like_windows {
|
|
|
|
self.link_arg("--as-needed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
|
-Z linker-flavor
This patch adds a `-Z linker-flavor` flag to rustc which can be used to invoke
the linker using a different interface.
For example, by default rustc assumes that all the Linux targets will be linked
using GCC. This makes it impossible to use LLD as a linker using just `-C
linker=ld.lld` because that will invoke LLD with invalid command line
arguments. (e.g. rustc will pass -Wl,--gc-sections to LLD but LLD doesn't
understand that; --gc-sections would be the right argument)
With this patch one can pass `-Z linker-flavor=ld` to rustc to invoke the linker
using a LD-like interface. This way, `rustc -C linker=ld.lld -Z
linker-flavor=ld` will invoke LLD with the right arguments.
`-Z linker-flavor` accepts 4 different arguments: `em` (emcc), `ld`,
`gcc`, `msvc` (link.exe). `em`, `gnu` and `msvc` cover all the existing linker
interfaces. `ld` is a new flavor for interfacing GNU's ld and LLD.
This patch also changes target specifications. `linker-flavor` is now a
mandatory field that specifies the *default* linker flavor that the target will
use. This change also makes the linker interface *explicit*; before, it used to
be derived from other fields like linker-is-gnu, is-like-msvc,
is-like-emscripten, etc.
Another change to target specifications is that the fields `pre-link-args`,
`post-link-args` and `late-link-args` now expect a map from flavor to linker
arguments.
``` diff
- "pre-link-args": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "pre-link-args": {
+ "gcc": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "ld": ["--as-needed", "-z,-noexecstack"],
+ },
```
[breaking-change] for users of custom targets specifications
2017-02-21 14:47:15 -05:00
|
|
|
impl<'a> Linker for GccLinker<'a> {
|
2020-04-05 02:11:29 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
|
2024-01-27 19:35:55 +03:00
|
|
|
fn is_cc(&self) -> bool {
|
|
|
|
!self.is_ld
|
|
|
|
}
|
|
|
|
|
2020-05-02 02:22:48 +03:00
|
|
|
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
|
|
|
|
match output_kind {
|
|
|
|
LinkOutputKind::DynamicNoPicExe => {
|
2022-08-06 21:08:46 +03:00
|
|
|
if !self.is_ld && self.is_gnu {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-no-pie");
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LinkOutputKind::DynamicPicExe => {
|
2021-05-18 03:57:53 -07:00
|
|
|
// noop on windows w/ gcc & ld, error w/ lld
|
|
|
|
if !self.sess.target.is_like_windows {
|
|
|
|
// `-pie` works for both gcc wrapper and ld.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg("-pie");
|
2021-05-18 03:57:53 -07:00
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
LinkOutputKind::StaticNoPicExe => {
|
|
|
|
// `-static` works for both gcc wrapper and ld.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg("-static");
|
2022-08-06 21:08:46 +03:00
|
|
|
if !self.is_ld && self.is_gnu {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-no-pie");
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LinkOutputKind::StaticPicExe => {
|
|
|
|
if !self.is_ld {
|
|
|
|
// Note that combination `-static -pie` doesn't work as expected
|
|
|
|
// for the gcc wrapper, `-static` in that case suppresses `-pie`.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-static-pie");
|
2020-05-02 02:22:48 +03:00
|
|
|
} else {
|
|
|
|
// `--no-dynamic-linker` and `-z text` are not strictly necessary for producing
|
|
|
|
// a static pie, but currently passed because gcc and clang pass them.
|
|
|
|
// The former suppresses the `INTERP` ELF header specifying dynamic linker,
|
|
|
|
// which is otherwise implicitly injected by ld (but not lld).
|
|
|
|
// The latter doesn't change anything, only ensures that everything is pic.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
LinkOutputKind::DynamicDylib => self.build_dylib(out_filename),
|
|
|
|
LinkOutputKind::StaticDylib => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg("-static");
|
2020-05-02 02:22:48 +03:00
|
|
|
self.build_dylib(out_filename);
|
|
|
|
}
|
2020-12-12 21:38:23 -06:00
|
|
|
LinkOutputKind::WasiReactorExe => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["--entry", "_initialize"]);
|
2020-12-12 21:38:23 -06:00
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
2020-05-30 17:03:50 +03:00
|
|
|
// VxWorks compiler driver introduced `--static-crt` flag specifically for rustc,
|
|
|
|
// it switches linking for libc and similar system libraries to static without using
|
|
|
|
// any `#[link]` attributes in the `libc` crate, see #72782 for details.
|
|
|
|
// FIXME: Switch to using `#[link]` attributes in the `libc` crate
|
|
|
|
// similarly to other targets.
|
2020-11-08 14:57:55 +03:00
|
|
|
if self.sess.target.os == "vxworks"
|
2020-05-30 17:03:50 +03:00
|
|
|
&& matches!(
|
|
|
|
output_kind,
|
|
|
|
LinkOutputKind::StaticNoPicExe
|
|
|
|
| LinkOutputKind::StaticPicExe
|
|
|
|
| LinkOutputKind::StaticDylib
|
|
|
|
)
|
|
|
|
{
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("--static-crt");
|
2020-05-30 17:03:50 +03:00
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
|
2024-01-17 23:14:14 +03:00
|
|
|
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, as_needed: bool) {
|
|
|
|
if self.sess.target.os == "illumos" && name == "c" {
|
2021-05-06 17:01:50 -07:00
|
|
|
// libc will be added via late_link_args on illumos so that it will
|
|
|
|
// appear last in the library search order.
|
|
|
|
// FIXME: This should be replaced by a more complete and generic
|
|
|
|
// mechanism for controlling the order of library arguments passed
|
|
|
|
// to the linker.
|
|
|
|
return;
|
|
|
|
}
|
2019-09-05 11:23:45 +10:00
|
|
|
self.hint_dynamic();
|
2024-06-06 19:57:36 +03:00
|
|
|
self.with_as_needed(as_needed, |this| {
|
|
|
|
let colon = if verbatim && this.is_gnu { ":" } else { "" };
|
|
|
|
this.link_or_cc_arg(format!("-l{colon}{name}"));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn link_dylib_by_path(&mut self, path: &Path, as_needed: bool) {
|
|
|
|
self.hint_dynamic();
|
|
|
|
self.with_as_needed(as_needed, |this| {
|
|
|
|
this.link_or_cc_arg(path);
|
|
|
|
})
|
2019-09-05 11:23:45 +10:00
|
|
|
}
|
2015-05-08 15:31:23 -07:00
|
|
|
|
2024-01-17 23:14:14 +03:00
|
|
|
fn link_framework_by_name(&mut self, name: &str, _verbatim: bool, as_needed: bool) {
|
2017-03-23 18:03:39 -07:00
|
|
|
self.hint_dynamic();
|
2021-03-24 21:45:09 -07:00
|
|
|
if !as_needed {
|
|
|
|
// FIXME(81490): ld64 as of macOS 11 supports the -needed_framework
|
|
|
|
// flag but we have no way to detect that here.
|
2024-01-27 19:35:55 +03:00
|
|
|
// self.link_or_cc_arg("-needed_framework").link_or_cc_arg(name);
|
2024-01-04 11:28:14 +11:00
|
|
|
self.sess.dcx().emit_warn(errors::Ld64UnimplementedModifier);
|
2021-03-24 21:45:09 -07:00
|
|
|
}
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_args(&["-framework", name]);
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
|
2017-03-23 18:03:39 -07:00
|
|
|
self.hint_static();
|
2024-01-18 18:09:59 +03:00
|
|
|
let colon = if verbatim && self.is_gnu { ":" } else { "" };
|
2024-01-18 17:41:18 +03:00
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(format!("-l{colon}{name}"));
|
2024-01-18 18:09:59 +03:00
|
|
|
} else if self.sess.target.is_like_osx {
|
2017-03-12 14:13:35 -04:00
|
|
|
// -force_load is the macOS equivalent of --whole-archive, but it
|
2015-05-08 15:31:23 -07:00
|
|
|
// involves passing the full path to the library to link.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-force_load");
|
|
|
|
self.link_arg(find_native_static_library(name, verbatim, self.sess));
|
2024-01-18 18:09:59 +03:00
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--whole-archive")
|
|
|
|
.link_or_cc_arg(format!("-l{colon}{name}"))
|
|
|
|
.link_arg("--no-whole-archive");
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
self.hint_static();
|
2024-01-18 17:41:18 +03:00
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(path);
|
2024-01-18 17:41:18 +03:00
|
|
|
} else if self.sess.target.is_like_osx {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-force_load").link_arg(path);
|
2015-07-07 21:33:44 -07:00
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--whole-archive").link_arg(path).link_arg("--no-whole-archive");
|
2015-07-07 21:33:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:47:41 +03:00
|
|
|
fn framework_path(&mut self, path: &Path) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg("-F").link_or_cc_arg(path);
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
fn full_relro(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "relro", "-z", "now"]);
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
fn partial_relro(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "relro"]);
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
fn no_relro(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "norelro"]);
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
|
2016-05-10 14:17:57 -07:00
|
|
|
fn gc_sections(&mut self, keep_metadata: bool) {
|
2015-05-08 15:31:23 -07:00
|
|
|
// The dead_strip option to the linker specifies that functions and data
|
|
|
|
// unreachable by the entry point will be removed. This is quite useful
|
|
|
|
// with Rust's compilation model of compiling libraries at a time into
|
|
|
|
// one object file. For example, this brings hello world from 1.7MB to
|
|
|
|
// 458K.
|
|
|
|
//
|
|
|
|
// Note that this is done for both executables and dynamic libraries. We
|
|
|
|
// won't get much benefit from dylibs because LLVM will have already
|
|
|
|
// stripped away as much as it could. This has not been seen to impact
|
|
|
|
// link times negatively.
|
|
|
|
//
|
|
|
|
// -dead_strip can't be part of the pre_link_args because it's also used
|
2022-11-16 20:34:16 +00:00
|
|
|
// for partial linking when using multiple codegen units (-r). So we
|
2015-05-08 15:31:23 -07:00
|
|
|
// insert it here.
|
2020-11-08 14:27:51 +03:00
|
|
|
if self.sess.target.is_like_osx {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-dead_strip");
|
2015-05-08 15:31:23 -07:00
|
|
|
|
|
|
|
// If we're building a dylib, we don't use --gc-sections because LLVM
|
|
|
|
// has already done the best it can do, and we also don't want to
|
|
|
|
// eliminate the metadata. If we're building an executable, however,
|
|
|
|
// --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
|
|
|
|
// reduction.
|
2022-08-06 21:08:46 +03:00
|
|
|
} else if (self.is_gnu || self.sess.target.is_like_wasm) && !keep_metadata {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--gc-sections");
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:45:09 -07:00
|
|
|
fn no_gc_sections(&mut self) {
|
2022-08-06 21:08:46 +03:00
|
|
|
if self.is_gnu || self.sess.target.is_like_wasm {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--no-gc-sections");
|
2021-03-24 21:45:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:31:23 -07:00
|
|
|
fn optimize(&mut self) {
|
2022-08-06 21:08:46 +03:00
|
|
|
if !self.is_gnu && !self.sess.target.is_like_wasm {
|
2015-05-08 15:31:23 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GNU-style linkers support optimization with -O. GNU ld doesn't
|
|
|
|
// need a numeric argument, but other linkers do.
|
2015-12-31 16:50:06 +13:00
|
|
|
if self.sess.opts.optimize == config::OptLevel::Default
|
|
|
|
|| self.sess.opts.optimize == config::OptLevel::Aggressive
|
|
|
|
{
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-O1");
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:29:58 +01:00
|
|
|
fn pgo_gen(&mut self) {
|
2022-08-06 21:08:46 +03:00
|
|
|
if !self.is_gnu {
|
2018-03-19 22:29:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're doing PGO generation stuff and on a GNU-like linker, use the
|
|
|
|
// "-u" flag to properly pull in the profiler runtime bits.
|
|
|
|
//
|
|
|
|
// This is because LLVM otherwise won't add the needed initialization
|
|
|
|
// for us on Linux (though the extra flag should be harmless if it
|
|
|
|
// does).
|
|
|
|
//
|
|
|
|
// See https://reviews.llvm.org/D14033 and https://reviews.llvm.org/D14030.
|
|
|
|
//
|
|
|
|
// Though it may be worth to try to revert those changes upstream, since
|
|
|
|
// the overhead of the initialization should be minor.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_args(&["-u", "__llvm_profile_runtime"]);
|
2018-03-19 22:29:58 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 16:10:42 +01:00
|
|
|
fn control_flow_guard(&mut self) {}
|
2020-01-13 13:25:39 +00:00
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
2022-04-25 18:02:43 -07:00
|
|
|
fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
|
2021-02-12 10:24:16 -08:00
|
|
|
// MacOS linker doesn't support stripping symbols directly anymore.
|
|
|
|
if self.sess.target.is_like_osx {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-03 12:36:12 +08:00
|
|
|
match strip {
|
|
|
|
Strip::None => {}
|
|
|
|
Strip::Debuginfo => {
|
2022-09-28 16:01:12 +00:00
|
|
|
// The illumos linker does not support --strip-debug although
|
|
|
|
// it does support --strip-all as a compatibility alias for -s.
|
|
|
|
// The --strip-debug case is handled by running an external
|
|
|
|
// `strip` utility as a separate step after linking.
|
2024-02-21 16:49:01 +01:00
|
|
|
if !self.sess.target.is_like_solaris {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--strip-debug");
|
2022-09-28 16:01:12 +00:00
|
|
|
}
|
2018-10-06 11:49:03 +02:00
|
|
|
}
|
2020-05-03 12:36:12 +08:00
|
|
|
Strip::Symbols => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--strip-all");
|
2020-05-03 12:36:12 +08:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 16:11:00 -04:00
|
|
|
match self.sess.opts.unstable_opts.debuginfo_compression {
|
|
|
|
config::DebugInfoCompression::None => {}
|
|
|
|
config::DebugInfoCompression::Zlib => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--compress-debug-sections=zlib");
|
2023-10-12 16:11:00 -04:00
|
|
|
}
|
|
|
|
config::DebugInfoCompression::Zstd => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--compress-debug-sections=zstd");
|
2023-10-12 16:11:00 -04:00
|
|
|
}
|
|
|
|
}
|
2015-07-04 23:25:38 +02:00
|
|
|
}
|
|
|
|
|
2020-04-29 20:47:07 +03:00
|
|
|
fn no_crt_objects(&mut self) {
|
|
|
|
if !self.is_ld {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-nostartfiles");
|
2020-04-29 20:47:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 15:31:23 -07:00
|
|
|
fn no_default_libraries(&mut self) {
|
-Z linker-flavor
This patch adds a `-Z linker-flavor` flag to rustc which can be used to invoke
the linker using a different interface.
For example, by default rustc assumes that all the Linux targets will be linked
using GCC. This makes it impossible to use LLD as a linker using just `-C
linker=ld.lld` because that will invoke LLD with invalid command line
arguments. (e.g. rustc will pass -Wl,--gc-sections to LLD but LLD doesn't
understand that; --gc-sections would be the right argument)
With this patch one can pass `-Z linker-flavor=ld` to rustc to invoke the linker
using a LD-like interface. This way, `rustc -C linker=ld.lld -Z
linker-flavor=ld` will invoke LLD with the right arguments.
`-Z linker-flavor` accepts 4 different arguments: `em` (emcc), `ld`,
`gcc`, `msvc` (link.exe). `em`, `gnu` and `msvc` cover all the existing linker
interfaces. `ld` is a new flavor for interfacing GNU's ld and LLD.
This patch also changes target specifications. `linker-flavor` is now a
mandatory field that specifies the *default* linker flavor that the target will
use. This change also makes the linker interface *explicit*; before, it used to
be derived from other fields like linker-is-gnu, is-like-msvc,
is-like-emscripten, etc.
Another change to target specifications is that the fields `pre-link-args`,
`post-link-args` and `late-link-args` now expect a map from flavor to linker
arguments.
``` diff
- "pre-link-args": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "pre-link-args": {
+ "gcc": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "ld": ["--as-needed", "-z,-noexecstack"],
+ },
```
[breaking-change] for users of custom targets specifications
2017-02-21 14:47:15 -05:00
|
|
|
if !self.is_ld {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-nodefaultlibs");
|
-Z linker-flavor
This patch adds a `-Z linker-flavor` flag to rustc which can be used to invoke
the linker using a different interface.
For example, by default rustc assumes that all the Linux targets will be linked
using GCC. This makes it impossible to use LLD as a linker using just `-C
linker=ld.lld` because that will invoke LLD with invalid command line
arguments. (e.g. rustc will pass -Wl,--gc-sections to LLD but LLD doesn't
understand that; --gc-sections would be the right argument)
With this patch one can pass `-Z linker-flavor=ld` to rustc to invoke the linker
using a LD-like interface. This way, `rustc -C linker=ld.lld -Z
linker-flavor=ld` will invoke LLD with the right arguments.
`-Z linker-flavor` accepts 4 different arguments: `em` (emcc), `ld`,
`gcc`, `msvc` (link.exe). `em`, `gnu` and `msvc` cover all the existing linker
interfaces. `ld` is a new flavor for interfacing GNU's ld and LLD.
This patch also changes target specifications. `linker-flavor` is now a
mandatory field that specifies the *default* linker flavor that the target will
use. This change also makes the linker interface *explicit*; before, it used to
be derived from other fields like linker-is-gnu, is-like-msvc,
is-like-emscripten, etc.
Another change to target specifications is that the fields `pre-link-args`,
`post-link-args` and `late-link-args` now expect a map from flavor to linker
arguments.
``` diff
- "pre-link-args": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "pre-link-args": {
+ "gcc": ["-Wl,--as-needed", "-Wl,-z,-noexecstack"],
+ "ld": ["--as-needed", "-z,-noexecstack"],
+ },
```
[breaking-change] for users of custom targets specifications
2017-02-21 14:47:15 -05:00
|
|
|
}
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
|
|
|
|
2021-07-06 18:28:07 +02:00
|
|
|
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
|
2019-05-03 13:59:50 -07:00
|
|
|
// Symbol visibility in object files typically takes care of this.
|
2022-07-25 05:20:23 +00:00
|
|
|
if crate_type == CrateType::Executable {
|
|
|
|
let should_export_executable_symbols =
|
|
|
|
self.sess.opts.unstable_opts.export_executable_symbols;
|
|
|
|
if self.sess.target.override_export_symbols.is_none()
|
|
|
|
&& !should_export_executable_symbols
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-05-03 13:59:50 -07:00
|
|
|
}
|
|
|
|
|
2019-04-06 18:07:53 +02:00
|
|
|
// We manually create a list of exported symbols to ensure we don't expose any more.
|
|
|
|
// The object files have far more public symbols than we actually want to export,
|
|
|
|
// so we hide them all here.
|
2016-05-10 14:17:57 -07:00
|
|
|
|
2020-11-08 14:27:51 +03:00
|
|
|
if !self.sess.target.limit_rdylib_exports {
|
2019-03-19 13:06:37 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-08 14:27:51 +03:00
|
|
|
let is_windows = self.sess.target.is_like_windows;
|
2020-05-07 11:52:21 +02:00
|
|
|
let path = tmpdir.join(if is_windows { "list.def" } else { "list" });
|
2016-08-13 14:09:43 +05:00
|
|
|
|
2016-12-01 14:57:46 -05:00
|
|
|
debug!("EXPORTED SYMBOLS:");
|
|
|
|
|
2020-11-08 14:27:51 +03:00
|
|
|
if self.sess.target.is_like_osx {
|
2016-11-30 13:16:53 -05:00
|
|
|
// Write a plain, newline-separated list of symbols
|
2019-03-14 23:12:56 +09:00
|
|
|
let res: io::Result<()> = try {
|
2016-08-13 14:09:43 +05:00
|
|
|
let mut f = BufWriter::new(File::create(&path)?);
|
2021-07-06 18:28:07 +02:00
|
|
|
for sym in symbols {
|
2023-07-25 23:04:01 +02:00
|
|
|
debug!(" _{sym}");
|
|
|
|
writeln!(f, "_{sym}")?;
|
2016-08-13 14:09:43 +05:00
|
|
|
}
|
2019-03-14 23:12:56 +09:00
|
|
|
};
|
2022-08-26 20:21:55 -04:00
|
|
|
if let Err(error) = res {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
|
2016-05-10 14:17:57 -07:00
|
|
|
}
|
2020-05-07 11:52:21 +02:00
|
|
|
} else if is_windows {
|
|
|
|
let res: io::Result<()> = try {
|
|
|
|
let mut f = BufWriter::new(File::create(&path)?);
|
|
|
|
|
|
|
|
// .def file similar to MSVC one but without LIBRARY section
|
|
|
|
// because LD doesn't like when it's empty
|
|
|
|
writeln!(f, "EXPORTS")?;
|
2021-07-06 18:28:07 +02:00
|
|
|
for symbol in symbols {
|
2023-07-25 23:04:01 +02:00
|
|
|
debug!(" _{symbol}");
|
|
|
|
writeln!(f, " {symbol}")?;
|
2020-05-07 11:52:21 +02:00
|
|
|
}
|
|
|
|
};
|
2022-08-26 20:21:55 -04:00
|
|
|
if let Err(error) = res {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
|
2020-05-07 11:52:21 +02:00
|
|
|
}
|
2016-05-10 14:17:57 -07:00
|
|
|
} else {
|
2016-11-30 13:16:53 -05:00
|
|
|
// Write an LD version script
|
2019-03-14 23:12:56 +09:00
|
|
|
let res: io::Result<()> = try {
|
2016-08-13 14:09:43 +05:00
|
|
|
let mut f = BufWriter::new(File::create(&path)?);
|
2019-08-27 20:35:33 -04:00
|
|
|
writeln!(f, "{{")?;
|
2021-07-06 18:28:07 +02:00
|
|
|
if !symbols.is_empty() {
|
2019-08-27 20:35:33 -04:00
|
|
|
writeln!(f, " global:")?;
|
2021-07-06 18:28:07 +02:00
|
|
|
for sym in symbols {
|
2023-07-25 23:04:01 +02:00
|
|
|
debug!(" {sym};");
|
|
|
|
writeln!(f, " {sym};")?;
|
2019-08-27 20:35:33 -04:00
|
|
|
}
|
2016-08-13 14:09:43 +05:00
|
|
|
}
|
2016-11-30 13:16:53 -05:00
|
|
|
writeln!(f, "\n local:\n *;\n}};")?;
|
2019-03-14 23:12:56 +09:00
|
|
|
};
|
2022-10-07 10:03:45 -04:00
|
|
|
if let Err(error) = res {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.sess.dcx().emit_fatal(errors::VersionScriptWriteFailure { error });
|
2016-08-13 14:09:43 +05:00
|
|
|
}
|
2016-05-10 14:17:57 -07:00
|
|
|
}
|
2016-08-13 14:09:43 +05:00
|
|
|
|
2020-11-08 14:27:51 +03:00
|
|
|
if self.sess.target.is_like_osx {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-exported_symbols_list").link_arg(path);
|
2020-11-08 14:27:51 +03:00
|
|
|
} else if self.sess.target.is_like_solaris {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-M").link_arg(path);
|
2016-11-30 13:16:53 -05:00
|
|
|
} else {
|
2021-11-09 10:36:47 +11:00
|
|
|
if is_windows {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(path);
|
2021-11-09 10:36:47 +11:00
|
|
|
} else {
|
|
|
|
let mut arg = OsString::from("--version-script=");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(arg).link_arg("--no-undefined-version");
|
2020-05-07 11:52:21 +02:00
|
|
|
}
|
2016-11-30 13:16:53 -05:00
|
|
|
}
|
2015-07-28 17:19:08 -07:00
|
|
|
}
|
2016-10-31 09:36:30 -07:00
|
|
|
|
|
|
|
fn subsystem(&mut self, subsystem: &str) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["--subsystem", subsystem]);
|
2016-10-31 09:36:30 -07:00
|
|
|
}
|
2017-03-23 18:03:39 -07:00
|
|
|
|
2021-05-08 20:38:13 +03:00
|
|
|
fn reset_per_library_state(&mut self) {
|
2017-03-23 18:03:39 -07:00
|
|
|
self.hint_dynamic(); // Reset to default before returning the composed command line.
|
|
|
|
}
|
2018-03-23 14:33:22 -07:00
|
|
|
|
2019-02-01 15:15:43 +01:00
|
|
|
fn linker_plugin_lto(&mut self) {
|
|
|
|
match self.sess.opts.cg.linker_plugin_lto {
|
|
|
|
LinkerPluginLto::Disabled => {
|
2018-04-25 15:45:04 +02:00
|
|
|
// Nothing to do
|
|
|
|
}
|
2019-02-01 15:15:43 +01:00
|
|
|
LinkerPluginLto::LinkerPluginAuto => {
|
|
|
|
self.push_linker_plugin_lto_args(None);
|
2018-07-03 16:33:11 +02:00
|
|
|
}
|
2019-02-01 15:15:43 +01:00
|
|
|
LinkerPluginLto::LinkerPlugin(ref path) => {
|
|
|
|
self.push_linker_plugin_lto_args(Some(path.as_os_str()));
|
2018-04-25 15:45:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 23:26:24 +03:00
|
|
|
|
|
|
|
// Add the `GNU_EH_FRAME` program header which is required to locate unwinding information.
|
|
|
|
// Some versions of `gcc` add it implicitly, some (e.g. `musl-gcc`) don't,
|
|
|
|
// so we just always add it.
|
|
|
|
fn add_eh_frame_header(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--eh-frame-hdr");
|
2020-06-20 23:26:24 +03:00
|
|
|
}
|
2021-03-28 00:02:23 +03:00
|
|
|
|
2021-03-28 23:18:39 +03:00
|
|
|
fn add_no_exec(&mut self) {
|
|
|
|
if self.sess.target.is_like_windows {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--nxcompat");
|
2022-08-06 21:08:46 +03:00
|
|
|
} else if self.is_gnu {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "noexecstack"]);
|
2021-03-28 23:18:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 00:02:23 +03:00
|
|
|
fn add_as_needed(&mut self) {
|
2022-08-06 21:08:46 +03:00
|
|
|
if self.is_gnu && !self.sess.target.is_like_windows {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--as-needed");
|
2021-04-30 15:53:14 -07:00
|
|
|
} else if self.sess.target.is_like_solaris {
|
|
|
|
// -z ignore is the Solaris equivalent to the GNU ld --as-needed option
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "ignore"]);
|
2021-03-28 00:02:23 +03:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 15:31:23 -07:00
|
|
|
}
|
2015-05-11 14:57:21 -07:00
|
|
|
|
|
|
|
pub struct MsvcLinker<'a> {
|
2017-03-23 18:03:39 -07:00
|
|
|
cmd: Command,
|
2016-05-25 01:45:25 +03:00
|
|
|
sess: &'a Session,
|
2015-05-11 14:57:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for MsvcLinker<'a> {
|
2020-04-05 02:11:29 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
|
|
|
|
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
|
|
|
|
match output_kind {
|
|
|
|
LinkOutputKind::DynamicNoPicExe
|
|
|
|
| LinkOutputKind::DynamicPicExe
|
|
|
|
| LinkOutputKind::StaticNoPicExe
|
|
|
|
| LinkOutputKind::StaticPicExe => {}
|
|
|
|
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/DLL");
|
2020-05-02 02:22:48 +03:00
|
|
|
let mut arg: OsString = "/IMPLIB:".into();
|
|
|
|
arg.push(out_filename.with_extension("dll.lib"));
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(arg);
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
2020-12-12 21:38:23 -06:00
|
|
|
LinkOutputKind::WasiReactorExe => {
|
|
|
|
panic!("can't link as reactor on non-wasi target");
|
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:47:41 +03:00
|
|
|
fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
|
2024-06-06 19:57:36 +03:00
|
|
|
fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
|
|
|
|
// When producing a dll, MSVC linker may not emit an implib file if the dll doesn't export
|
|
|
|
// any symbols, so we skip linking if the implib file is not present.
|
|
|
|
let implib_path = path.with_extension("dll.lib");
|
|
|
|
if implib_path.exists() {
|
|
|
|
self.link_or_cc_arg(implib_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
|
2024-01-18 18:09:59 +03:00
|
|
|
let prefix = if whole_archive { "/WHOLEARCHIVE:" } else { "" };
|
|
|
|
let suffix = if verbatim { "" } else { ".lib" };
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(format!("{prefix}{name}{suffix}"));
|
2015-05-11 14:57:21 -07:00
|
|
|
}
|
2024-01-18 15:47:41 +03:00
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
|
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(path);
|
2024-01-18 17:41:18 +03:00
|
|
|
} else {
|
|
|
|
let mut arg = OsString::from("/WHOLEARCHIVE:");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(arg);
|
2024-01-18 17:41:18 +03:00
|
|
|
}
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
|
2016-05-10 14:17:57 -07:00
|
|
|
fn gc_sections(&mut self, _keep_metadata: bool) {
|
2016-11-25 19:17:21 +11:00
|
|
|
// MSVC's ICF (Identical COMDAT Folding) link optimization is
|
|
|
|
// slow for Rust and thus we disable it by default when not in
|
|
|
|
// optimization build.
|
|
|
|
if self.sess.opts.optimize != config::OptLevel::No {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/OPT:REF,ICF");
|
2016-11-25 19:17:21 +11:00
|
|
|
} else {
|
|
|
|
// It is necessary to specify NOICF here, because /OPT:REF
|
|
|
|
// implies ICF by default.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/OPT:REF,NOICF");
|
2016-11-25 19:17:21 +11:00
|
|
|
}
|
2016-05-10 14:17:57 -07:00
|
|
|
}
|
2015-05-11 14:57:21 -07:00
|
|
|
|
2021-03-24 21:45:09 -07:00
|
|
|
fn no_gc_sections(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/OPT:NOREF,NOICF");
|
2021-03-24 21:45:09 -07:00
|
|
|
}
|
|
|
|
|
2018-02-22 21:28:20 +01:00
|
|
|
fn full_relro(&mut self) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:01:37 +02:00
|
|
|
fn partial_relro(&mut self) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:28:20 +01:00
|
|
|
fn no_relro(&mut self) {
|
2017-07-10 20:57:45 +02:00
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:47:07 +03:00
|
|
|
fn no_crt_objects(&mut self) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2015-05-11 14:57:21 -07:00
|
|
|
fn no_default_libraries(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/NODEFAULTLIB");
|
2015-05-11 14:57:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn include_path(&mut self, path: &Path) {
|
|
|
|
let mut arg = OsString::from("/LIBPATH:");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&arg);
|
2015-05-11 14:57:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn output_filename(&mut self, path: &Path) {
|
|
|
|
let mut arg = OsString::from("/OUT:");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&arg);
|
2015-05-11 14:57:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn optimize(&mut self) {
|
|
|
|
// Needs more investigation of `/OPT` arguments
|
|
|
|
}
|
2015-07-04 23:25:38 +02:00
|
|
|
|
2018-03-19 22:29:58 +01:00
|
|
|
fn pgo_gen(&mut self) {
|
|
|
|
// Nothing needed here.
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:25:39 +00:00
|
|
|
fn control_flow_guard(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/guard:cf");
|
2020-01-13 13:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {
|
|
|
|
if self.sess.target.pointer_width == 64 {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/guard:ehcont");
|
2023-11-17 10:05:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 08:18:01 +09:00
|
|
|
fn debuginfo(&mut self, _strip: Strip, natvis_debugger_visualizers: &[PathBuf]) {
|
|
|
|
// This will cause the Microsoft linker to generate a PDB file
|
|
|
|
// from the CodeView line tables in the object files.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/DEBUG");
|
2024-04-05 08:18:01 +09:00
|
|
|
|
|
|
|
// Default to emitting only the file name of the PDB file into
|
|
|
|
// the binary instead of the full path. Emitting the full path
|
|
|
|
// may leak private information (such as user names).
|
|
|
|
// See https://github.com/rust-lang/rust/issues/87825.
|
|
|
|
//
|
|
|
|
// This default behavior can be overridden by explicitly passing
|
|
|
|
// `-Clink-arg=/PDBALTPATH:...` to rustc.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/PDBALTPATH:%_PDB%");
|
2024-04-05 08:18:01 +09:00
|
|
|
|
|
|
|
// This will cause the Microsoft linker to embed .natvis info into the PDB file
|
|
|
|
let natvis_dir_path = self.sess.sysroot.join("lib\\rustlib\\etc");
|
|
|
|
if let Ok(natvis_dir) = fs::read_dir(&natvis_dir_path) {
|
|
|
|
for entry in natvis_dir {
|
|
|
|
match entry {
|
|
|
|
Ok(entry) => {
|
|
|
|
let path = entry.path();
|
|
|
|
if path.extension() == Some("natvis".as_ref()) {
|
|
|
|
let mut arg = OsString::from("/NATVIS:");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(arg);
|
2017-07-13 10:03:07 -07:00
|
|
|
}
|
|
|
|
}
|
2024-04-05 08:18:01 +09:00
|
|
|
Err(error) => {
|
|
|
|
self.sess.dcx().emit_warn(errors::NoNatvisDirectory { error });
|
|
|
|
}
|
2022-04-25 18:02:43 -07:00
|
|
|
}
|
2017-07-13 10:03:07 -07:00
|
|
|
}
|
2024-04-05 08:18:01 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// This will cause the Microsoft linker to embed .natvis info for all crates into the PDB file
|
|
|
|
for path in natvis_debugger_visualizers {
|
|
|
|
let mut arg = OsString::from("/NATVIS:");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(arg);
|
2017-07-13 10:03:07 -07:00
|
|
|
}
|
2015-07-04 23:25:38 +02:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:19:08 -07:00
|
|
|
// Currently the compiler doesn't use `dllexport` (an LLVM attribute) to
|
|
|
|
// export symbols from a dynamic library. When building a dynamic library,
|
|
|
|
// however, we're going to want some symbols exported, so this function
|
|
|
|
// generates a DEF file which lists all the symbols.
|
|
|
|
//
|
|
|
|
// The linker will read this `*.def` file and export all the symbols from
|
|
|
|
// the dynamic library. Note that this is not as simple as just exporting
|
2018-05-08 16:10:16 +03:00
|
|
|
// all the symbols in the current crate (as specified by `codegen.reachable`)
|
2015-07-28 17:19:08 -07:00
|
|
|
// but rather we also need to possibly export the symbols of upstream
|
|
|
|
// crates. Upstream rlibs may be linked statically to this dynamic library,
|
|
|
|
// in which case they may continue to transitively be used and hence need
|
|
|
|
// their symbols exported.
|
2021-07-06 18:28:07 +02:00
|
|
|
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType, symbols: &[String]) {
|
2019-05-03 13:59:50 -07:00
|
|
|
// Symbol visibility takes care of this typically
|
|
|
|
if crate_type == CrateType::Executable {
|
2022-07-25 05:20:23 +00:00
|
|
|
let should_export_executable_symbols =
|
|
|
|
self.sess.opts.unstable_opts.export_executable_symbols;
|
|
|
|
if !should_export_executable_symbols {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-03 13:59:50 -07:00
|
|
|
}
|
|
|
|
|
2015-07-28 17:19:08 -07:00
|
|
|
let path = tmpdir.join("lib.def");
|
2019-03-14 23:12:56 +09:00
|
|
|
let res: io::Result<()> = try {
|
2016-03-22 22:01:37 -05:00
|
|
|
let mut f = BufWriter::new(File::create(&path)?);
|
2015-07-28 17:19:08 -07:00
|
|
|
|
|
|
|
// Start off with the standard module name header and then go
|
|
|
|
// straight to exports.
|
2016-03-22 22:01:37 -05:00
|
|
|
writeln!(f, "LIBRARY")?;
|
|
|
|
writeln!(f, "EXPORTS")?;
|
2021-07-06 18:28:07 +02:00
|
|
|
for symbol in symbols {
|
2023-07-25 23:04:01 +02:00
|
|
|
debug!(" _{symbol}");
|
|
|
|
writeln!(f, " {symbol}")?;
|
2015-07-28 17:19:08 -07:00
|
|
|
}
|
2019-03-14 23:12:56 +09:00
|
|
|
};
|
2022-08-26 20:21:55 -04:00
|
|
|
if let Err(error) = res {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.sess.dcx().emit_fatal(errors::LibDefWriteFailure { error });
|
2015-07-28 17:19:08 -07:00
|
|
|
}
|
|
|
|
let mut arg = OsString::from("/DEF:");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&arg);
|
2015-07-28 17:19:08 -07:00
|
|
|
}
|
2016-10-31 09:36:30 -07:00
|
|
|
|
|
|
|
fn subsystem(&mut self, subsystem: &str) {
|
|
|
|
// Note that previous passes of the compiler validated this subsystem,
|
|
|
|
// so we just blindly pass it to the linker.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&format!("/SUBSYSTEM:{subsystem}"));
|
2016-10-31 09:36:30 -07:00
|
|
|
|
|
|
|
// Windows has two subsystems we're interested in right now, the console
|
|
|
|
// and windows subsystems. These both implicitly have different entry
|
|
|
|
// points (starting symbols). The console entry point starts with
|
|
|
|
// `mainCRTStartup` and the windows entry point starts with
|
|
|
|
// `WinMainCRTStartup`. These entry points, defined in system libraries,
|
|
|
|
// will then later probe for either `main` or `WinMain`, respectively to
|
|
|
|
// start the application.
|
|
|
|
//
|
|
|
|
// In Rust we just always generate a `main` function so we want control
|
|
|
|
// to always start there, so we force the entry point on the windows
|
|
|
|
// subsystem to be `mainCRTStartup` to get everything booted up
|
|
|
|
// correctly.
|
|
|
|
//
|
|
|
|
// For more information see RFC #1665
|
|
|
|
if subsystem == "windows" {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/ENTRY:mainCRTStartup");
|
2016-10-31 09:36:30 -07:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 18:03:39 -07:00
|
|
|
|
2019-02-01 15:15:43 +01:00
|
|
|
fn linker_plugin_lto(&mut self) {
|
2018-04-25 15:45:04 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
2021-03-28 23:18:39 +03:00
|
|
|
|
|
|
|
fn add_no_exec(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("/NXCOMPAT");
|
2021-03-28 23:18:39 +03:00
|
|
|
}
|
2015-05-11 14:57:21 -07:00
|
|
|
}
|
2016-05-10 14:17:57 -07:00
|
|
|
|
2017-02-03 14:58:13 +00:00
|
|
|
pub struct EmLinker<'a> {
|
2017-03-23 18:03:39 -07:00
|
|
|
cmd: Command,
|
2017-02-03 14:58:13 +00:00
|
|
|
sess: &'a Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for EmLinker<'a> {
|
2020-04-05 02:11:29 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
|
2024-01-27 19:35:55 +03:00
|
|
|
fn is_cc(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2020-05-02 02:22:48 +03:00
|
|
|
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
|
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
// Emscripten always links statically
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_args(&["-l", name]);
|
2017-02-03 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-06 19:57:36 +03:00
|
|
|
fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
|
|
|
|
self.link_or_cc_arg(path);
|
|
|
|
}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, _whole_archive: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_args(&["-l", name]);
|
2017-02-03 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(path);
|
2017-02-03 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-22 21:28:20 +01:00
|
|
|
fn full_relro(&mut self) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2017-07-14 22:01:37 +02:00
|
|
|
fn partial_relro(&mut self) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2018-02-22 21:28:20 +01:00
|
|
|
fn no_relro(&mut self) {
|
2017-07-10 20:57:45 +02:00
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:58:13 +00:00
|
|
|
fn gc_sections(&mut self, _keep_metadata: bool) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:45:09 -07:00
|
|
|
fn no_gc_sections(&mut self) {
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:58:13 +00:00
|
|
|
fn optimize(&mut self) {
|
|
|
|
// Emscripten performs own optimizations
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg(match self.sess.opts.optimize {
|
2017-02-03 14:58:13 +00:00
|
|
|
OptLevel::No => "-O0",
|
|
|
|
OptLevel::Less => "-O1",
|
|
|
|
OptLevel::Default => "-O2",
|
|
|
|
OptLevel::Aggressive => "-O3",
|
|
|
|
OptLevel::Size => "-Os",
|
|
|
|
OptLevel::SizeMin => "-Oz",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-19 22:29:58 +01:00
|
|
|
fn pgo_gen(&mut self) {
|
|
|
|
// noop, but maybe we need something like the gnu linker?
|
|
|
|
}
|
|
|
|
|
2020-07-06 16:10:42 +01:00
|
|
|
fn control_flow_guard(&mut self) {}
|
2020-01-13 13:25:39 +00:00
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
2022-04-25 18:02:43 -07:00
|
|
|
fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
|
2017-02-03 14:58:13 +00:00
|
|
|
// Preserve names or generate source maps depending on debug info
|
2021-04-06 16:00:35 -04:00
|
|
|
// For more information see https://emscripten.org/docs/tools_reference/emcc.html#emcc-g
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg(match self.sess.opts.debuginfo {
|
2018-07-26 11:41:10 -06:00
|
|
|
DebugInfo::None => "-g0",
|
2021-04-06 16:00:35 -04:00
|
|
|
DebugInfo::Limited | DebugInfo::LineTablesOnly | DebugInfo::LineDirectivesOnly => {
|
|
|
|
"--profiling-funcs"
|
|
|
|
}
|
2022-06-15 15:08:38 -07:00
|
|
|
DebugInfo::Full => "-g",
|
2017-02-03 14:58:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-29 20:47:07 +03:00
|
|
|
fn no_crt_objects(&mut self) {}
|
|
|
|
|
2017-02-03 14:58:13 +00:00
|
|
|
fn no_default_libraries(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-nodefaultlibs");
|
2017-02-03 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 18:28:07 +02:00
|
|
|
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
|
2017-02-03 14:58:13 +00:00
|
|
|
debug!("EXPORTED SYMBOLS:");
|
|
|
|
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-s");
|
2017-02-03 14:58:13 +00:00
|
|
|
|
|
|
|
let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
|
2022-05-06 17:20:42 +02:00
|
|
|
let encoded = serde_json::to_string(
|
|
|
|
&symbols.iter().map(|sym| "_".to_owned() + sym).collect::<Vec<_>>(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-07-25 23:04:01 +02:00
|
|
|
debug!("{encoded}");
|
2022-05-06 17:20:42 +02:00
|
|
|
|
2017-02-03 14:58:13 +00:00
|
|
|
arg.push(encoded);
|
|
|
|
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg(arg);
|
2017-02-03 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn subsystem(&mut self, _subsystem: &str) {
|
|
|
|
// noop
|
|
|
|
}
|
2017-03-23 18:03:39 -07:00
|
|
|
|
2019-02-01 15:15:43 +01:00
|
|
|
fn linker_plugin_lto(&mut self) {
|
2018-04-25 15:45:04 +02:00
|
|
|
// Do nothing
|
|
|
|
}
|
2017-02-03 14:58:13 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 10:20:00 -07:00
|
|
|
pub struct WasmLd<'a> {
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
cmd: Command,
|
2018-06-01 10:20:00 -07:00
|
|
|
sess: &'a Session,
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
}
|
|
|
|
|
2019-01-04 07:54:33 -08:00
|
|
|
impl<'a> WasmLd<'a> {
|
2024-01-27 19:35:55 +03:00
|
|
|
fn new(cmd: Command, sess: &'a Session) -> WasmLd<'a> {
|
2019-07-19 07:08:37 -07:00
|
|
|
// If the atomics feature is enabled for wasm then we need a whole bunch
|
|
|
|
// of flags:
|
|
|
|
//
|
|
|
|
// * `--shared-memory` - the link won't even succeed without this, flags
|
|
|
|
// the one linear memory as `shared`
|
|
|
|
//
|
|
|
|
// * `--max-memory=1G` - when specifying a shared memory this must also
|
|
|
|
// be specified. We conservatively choose 1GB but users should be able
|
|
|
|
// to override this with `-C link-arg`.
|
|
|
|
//
|
|
|
|
// * `--import-memory` - it doesn't make much sense for memory to be
|
|
|
|
// exported in a threaded module because typically you're
|
|
|
|
// sharing memory and instantiating the module multiple times. As a
|
|
|
|
// result if it were exported then we'd just have no sharing.
|
|
|
|
//
|
2022-09-28 14:40:53 -07:00
|
|
|
// On wasm32-unknown-unknown, we also export symbols for glue code to use:
|
|
|
|
// * `--export=*tls*` - when `#[thread_local]` symbols are used these
|
|
|
|
// symbols are how the TLS segments are initialized and configured.
|
2024-01-27 19:35:55 +03:00
|
|
|
let mut wasm_ld = WasmLd { cmd, sess };
|
2020-06-23 09:41:56 -07:00
|
|
|
if sess.target_features.contains(&sym::atomics) {
|
2024-01-27 19:35:55 +03:00
|
|
|
wasm_ld.link_args(&["--shared-memory", "--max-memory=1073741824", "--import-memory"]);
|
2022-09-28 14:40:53 -07:00
|
|
|
if sess.target.os == "unknown" {
|
2024-01-27 19:35:55 +03:00
|
|
|
wasm_ld.link_args(&[
|
|
|
|
"--export=__wasm_init_tls",
|
|
|
|
"--export=__tls_size",
|
|
|
|
"--export=__tls_align",
|
|
|
|
"--export=__tls_base",
|
|
|
|
]);
|
2022-09-28 14:40:53 -07:00
|
|
|
}
|
2019-07-19 07:08:37 -07:00
|
|
|
}
|
2024-01-27 19:35:55 +03:00
|
|
|
wasm_ld
|
2019-01-04 07:54:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 10:20:00 -07:00
|
|
|
impl<'a> Linker for WasmLd<'a> {
|
2020-04-05 02:11:29 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
|
|
|
|
2020-05-02 02:22:48 +03:00
|
|
|
fn set_output_kind(&mut self, output_kind: LinkOutputKind, _out_filename: &Path) {
|
|
|
|
match output_kind {
|
|
|
|
LinkOutputKind::DynamicNoPicExe
|
|
|
|
| LinkOutputKind::DynamicPicExe
|
|
|
|
| LinkOutputKind::StaticNoPicExe
|
|
|
|
| LinkOutputKind::StaticPicExe => {}
|
|
|
|
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--no-entry");
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
2020-12-12 21:38:23 -06:00
|
|
|
LinkOutputKind::WasiReactorExe => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["--entry", "_initialize"]);
|
2020-12-12 21:38:23 -06:00
|
|
|
}
|
2020-05-02 02:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 23:14:14 +03:00
|
|
|
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_args(&["-l", name]);
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
}
|
|
|
|
|
2024-06-06 19:57:36 +03:00
|
|
|
fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
|
|
|
|
self.link_or_cc_arg(path);
|
|
|
|
}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
|
2024-01-18 17:41:18 +03:00
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_args(&["-l", name]);
|
2024-01-18 17:41:18 +03:00
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--whole-archive")
|
|
|
|
.link_or_cc_args(&["-l", name])
|
|
|
|
.link_arg("--no-whole-archive");
|
2024-01-18 17:41:18 +03:00
|
|
|
}
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
}
|
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
|
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(path);
|
2024-01-18 17:41:18 +03:00
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
|
2024-01-18 17:41:18 +03:00
|
|
|
}
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
|
2018-02-22 21:28:20 +01:00
|
|
|
fn full_relro(&mut self) {}
|
|
|
|
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
fn partial_relro(&mut self) {}
|
|
|
|
|
2018-02-22 21:28:20 +01:00
|
|
|
fn no_relro(&mut self) {}
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
|
|
|
|
fn gc_sections(&mut self, _keep_metadata: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--gc-sections");
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
}
|
|
|
|
|
2021-03-24 21:45:09 -07:00
|
|
|
fn no_gc_sections(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--no-gc-sections");
|
2021-03-24 21:45:09 -07:00
|
|
|
}
|
|
|
|
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
fn optimize(&mut self) {
|
2023-11-28 01:54:38 +11:00
|
|
|
// The -O flag is, as of late 2023, only used for merging of strings and debuginfo, and
|
|
|
|
// only differentiates -O0 and -O1. It does not apply to LTO.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(match self.sess.opts.optimize {
|
2018-06-01 10:20:00 -07:00
|
|
|
OptLevel::No => "-O0",
|
|
|
|
OptLevel::Less => "-O1",
|
|
|
|
OptLevel::Default => "-O2",
|
|
|
|
OptLevel::Aggressive => "-O3",
|
|
|
|
// Currently LLD doesn't support `Os` and `Oz`, so pass through `O2`
|
|
|
|
// instead.
|
|
|
|
OptLevel::Size => "-O2",
|
|
|
|
OptLevel::SizeMin => "-O2",
|
|
|
|
});
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
}
|
|
|
|
|
2018-03-19 22:29:58 +01:00
|
|
|
fn pgo_gen(&mut self) {}
|
|
|
|
|
2022-04-25 18:02:43 -07:00
|
|
|
fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
|
2020-05-03 12:36:12 +08:00
|
|
|
match strip {
|
|
|
|
Strip::None => {}
|
|
|
|
Strip::Debuginfo => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--strip-debug");
|
2020-05-03 12:36:12 +08:00
|
|
|
}
|
|
|
|
Strip::Symbols => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--strip-all");
|
2020-05-03 12:36:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
|
2020-07-06 16:10:42 +01:00
|
|
|
fn control_flow_guard(&mut self) {}
|
2020-01-13 13:25:39 +00:00
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
2020-04-29 20:47:07 +03:00
|
|
|
fn no_crt_objects(&mut self) {}
|
|
|
|
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
fn no_default_libraries(&mut self) {}
|
|
|
|
|
2021-07-06 18:28:07 +02:00
|
|
|
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
|
|
|
|
for sym in symbols {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["--export", sym]);
|
2018-10-02 13:49:51 -07:00
|
|
|
}
|
2019-07-19 07:08:37 -07:00
|
|
|
|
2020-07-07 11:12:44 -04:00
|
|
|
// LLD will hide these otherwise-internal symbols since it only exports
|
2022-03-01 20:02:47 +08:00
|
|
|
// symbols explicitly passed via the `--export` flags above and hides all
|
2022-09-27 17:33:59 -07:00
|
|
|
// others. Various bits and pieces of wasm32-unknown-unknown tooling use
|
|
|
|
// this, so be sure these symbols make their way out of the linker as well.
|
|
|
|
if self.sess.target.os == "unknown" {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["--export=__heap_base", "--export=__data_end"]);
|
2022-09-27 17:33:59 -07:00
|
|
|
}
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn subsystem(&mut self, _subsystem: &str) {}
|
|
|
|
|
2019-02-01 15:15:43 +01:00
|
|
|
fn linker_plugin_lto(&mut self) {
|
2023-11-28 01:54:38 +11:00
|
|
|
match self.sess.opts.cg.linker_plugin_lto {
|
|
|
|
LinkerPluginLto::Disabled => {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
LinkerPluginLto::LinkerPluginAuto => {
|
|
|
|
self.push_linker_plugin_lto_args();
|
|
|
|
}
|
|
|
|
LinkerPluginLto::LinkerPlugin(_) => {
|
|
|
|
self.push_linker_plugin_lto_args();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> WasmLd<'a> {
|
|
|
|
fn push_linker_plugin_lto_args(&mut self) {
|
|
|
|
let opt_level = match self.sess.opts.optimize {
|
|
|
|
config::OptLevel::No => "O0",
|
|
|
|
config::OptLevel::Less => "O1",
|
|
|
|
config::OptLevel::Default => "O2",
|
|
|
|
config::OptLevel::Aggressive => "O3",
|
|
|
|
// wasm-ld only handles integer LTO opt levels. Use O2
|
|
|
|
config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
|
|
|
|
};
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&format!("--lto-{opt_level}"));
|
2018-04-25 15:45:04 +02:00
|
|
|
}
|
rust: Import LLD for linking wasm objects
This commit imports the LLD project from LLVM to serve as the default linker for
the `wasm32-unknown-unknown` target. The `binaryen` submoule is consequently
removed along with "binaryen linker" support in rustc.
Moving to LLD brings with it a number of benefits for wasm code:
* LLD is itself an actual linker, so there's no need to compile all wasm code
with LTO any more. As a result builds should be *much* speedier as LTO is no
longer forcibly enabled for all builds of the wasm target.
* LLD is quickly becoming an "official solution" for linking wasm code together.
This, I believe at least, is intended to be the main supported linker for
native code and wasm moving forward. Picking up support early on should help
ensure that we can help LLD identify bugs and otherwise prove that it works
great for all our use cases!
* Improvements to the wasm toolchain are currently primarily focused around LLVM
and LLD (from what I can tell at least), so it's in general much better to be
on this bandwagon for bugfixes and new features.
* Historical "hacks" like `wasm-gc` will soon no longer be necessary, LLD
will [natively implement][gc] `--gc-sections` (better than `wasm-gc`!) which
means a postprocessor is no longer needed to show off Rust's "small wasm
binary size".
LLD is added in a pretty standard way to rustc right now. A new rustbuild target
was defined for building LLD, and this is executed when a compiler's sysroot is
being assembled. LLD is compiled against the LLVM that we've got in tree, which
means we're currently on the `release_60` branch, but this may get upgraded in
the near future!
LLD is placed into rustc's sysroot in a `bin` directory. This is similar to
where `gcc.exe` can be found on Windows. This directory is automatically added
to `PATH` whenever rustc executes the linker, allowing us to define a `WasmLd`
linker which implements the interface that `wasm-ld`, LLD's frontend, expects.
Like Emscripten the LLD target is currently only enabled for Tier 1 platforms,
notably OSX/Windows/Linux, and will need to be installed manually for compiling
to wasm on other platforms. LLD is by default turned off in rustbuild, and
requires a `config.toml` option to be enabled to turn it on.
Finally the unstable `#![wasm_import_memory]` attribute was also removed as LLD
has a native option for controlling this.
[gc]: https://reviews.llvm.org/D42511
2017-08-26 18:30:12 -07:00
|
|
|
}
|
2018-10-23 17:01:35 +02:00
|
|
|
|
2018-04-03 14:53:13 +02:00
|
|
|
/// Linker shepherd script for L4Re (Fiasco)
|
|
|
|
pub struct L4Bender<'a> {
|
|
|
|
cmd: Command,
|
|
|
|
sess: &'a Session,
|
|
|
|
hinted_static: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for L4Bender<'a> {
|
2024-01-18 15:47:41 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool, whole_archive: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
self.hint_static();
|
2024-01-18 17:41:18 +03:00
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(format!("-PC{name}"));
|
2024-01-18 17:41:18 +03:00
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--whole-archive")
|
|
|
|
.link_or_cc_arg(format!("-l{name}"))
|
|
|
|
.link_arg("--no-whole-archive");
|
2024-01-18 17:41:18 +03:00
|
|
|
}
|
2018-04-03 14:53:13 +02:00
|
|
|
}
|
2024-01-18 15:47:41 +03:00
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
self.hint_static();
|
2024-01-18 17:41:18 +03:00
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(path);
|
2024-01-18 17:41:18 +03:00
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--whole-archive").link_or_cc_arg(path).link_arg("--no-whole-archive");
|
2024-01-18 17:41:18 +03:00
|
|
|
}
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn full_relro(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "relro", "-z", "now"]);
|
2021-05-31 14:34:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_relro(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "relro"]);
|
2021-05-31 14:34:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn no_relro(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-z", "norelro"]);
|
2021-05-31 14:34:23 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 14:53:13 +02:00
|
|
|
fn gc_sections(&mut self, keep_metadata: bool) {
|
|
|
|
if !keep_metadata {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--gc-sections");
|
2018-04-03 14:53:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn no_gc_sections(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--no-gc-sections");
|
2021-05-31 14:34:23 +02:00
|
|
|
}
|
|
|
|
|
2018-04-03 14:53:13 +02:00
|
|
|
fn optimize(&mut self) {
|
2021-05-31 14:34:23 +02:00
|
|
|
// GNU-style linkers support optimization with -O. GNU ld doesn't
|
|
|
|
// need a numeric argument, but other linkers do.
|
|
|
|
if self.sess.opts.optimize == config::OptLevel::Default
|
|
|
|
|| self.sess.opts.optimize == config::OptLevel::Aggressive
|
|
|
|
{
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-O1");
|
2021-05-31 14:34:23 +02:00
|
|
|
}
|
2018-04-03 14:53:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn pgo_gen(&mut self) {}
|
2018-04-03 14:53:13 +02:00
|
|
|
|
2022-04-25 18:02:43 -07:00
|
|
|
fn debuginfo(&mut self, strip: Strip, _: &[PathBuf]) {
|
2018-04-03 14:53:13 +02:00
|
|
|
match strip {
|
|
|
|
Strip::None => {}
|
|
|
|
Strip::Debuginfo => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--strip-debug");
|
2021-05-31 14:34:23 +02:00
|
|
|
}
|
2018-04-03 14:53:13 +02:00
|
|
|
Strip::Symbols => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--strip-all");
|
2018-04-03 14:53:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn no_default_libraries(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.cc_arg("-nostdlib");
|
2018-04-03 14:53:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn export_symbols(&mut self, _: &Path, _: CrateType, _: &[String]) {
|
2018-04-03 14:53:13 +02:00
|
|
|
// ToDo, not implemented, copy from GCC
|
2024-01-04 11:28:14 +11:00
|
|
|
self.sess.dcx().emit_warn(errors::L4BenderExportingSymbolsUnimplemented);
|
2018-04-03 14:53:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subsystem(&mut self, subsystem: &str) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(&format!("--subsystem {subsystem}"));
|
2018-04-03 14:53:13 +02:00
|
|
|
}
|
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn reset_per_library_state(&mut self) {
|
2018-04-03 14:53:13 +02:00
|
|
|
self.hint_static(); // Reset to default before returning the composed command line.
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn linker_plugin_lto(&mut self) {}
|
2018-04-03 14:53:13 +02:00
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn control_flow_guard(&mut self) {}
|
2018-04-03 14:53:13 +02:00
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
fn no_crt_objects(&mut self) {}
|
|
|
|
}
|
2018-04-03 14:53:13 +02:00
|
|
|
|
2021-05-31 14:34:23 +02:00
|
|
|
impl<'a> L4Bender<'a> {
|
|
|
|
pub fn new(cmd: Command, sess: &'a Session) -> L4Bender<'a> {
|
2024-01-27 19:35:55 +03:00
|
|
|
L4Bender { cmd, sess: sess, hinted_static: false }
|
2018-04-03 14:53:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hint_static(&mut self) {
|
|
|
|
if !self.hinted_static {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg("-static");
|
2018-04-03 14:53:13 +02:00
|
|
|
self.hinted_static = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-23 15:00:46 +08:00
|
|
|
/// Linker for AIX.
|
|
|
|
pub struct AixLinker<'a> {
|
|
|
|
cmd: Command,
|
|
|
|
sess: &'a Session,
|
2023-05-17 22:52:54 +02:00
|
|
|
hinted_static: Option<bool>,
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> AixLinker<'a> {
|
|
|
|
pub fn new(cmd: Command, sess: &'a Session) -> AixLinker<'a> {
|
2024-01-27 19:35:55 +03:00
|
|
|
AixLinker { cmd, sess: sess, hinted_static: None }
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hint_static(&mut self) {
|
2023-05-17 22:52:54 +02:00
|
|
|
if self.hinted_static != Some(true) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-bstatic");
|
2023-05-17 22:52:54 +02:00
|
|
|
self.hinted_static = Some(true);
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hint_dynamic(&mut self) {
|
2023-05-17 22:52:54 +02:00
|
|
|
if self.hinted_static != Some(false) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-bdynamic");
|
2023-05-17 22:52:54 +02:00
|
|
|
self.hinted_static = Some(false);
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn build_dylib(&mut self, _out_filename: &Path) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["-bM:SRE", "-bnoentry"]);
|
2023-03-23 15:00:46 +08:00
|
|
|
// FIXME: Use CreateExportList utility to create export list
|
|
|
|
// and remove -bexpfull.
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-bexpfull");
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for AixLinker<'a> {
|
2024-01-18 15:47:41 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) {
|
|
|
|
match output_kind {
|
|
|
|
LinkOutputKind::DynamicDylib => {
|
|
|
|
self.hint_dynamic();
|
|
|
|
self.build_dylib(out_filename);
|
|
|
|
}
|
|
|
|
LinkOutputKind::StaticDylib => {
|
|
|
|
self.hint_static();
|
|
|
|
self.build_dylib(out_filename);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-17 23:14:14 +03:00
|
|
|
fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
|
2023-03-23 15:00:46 +08:00
|
|
|
self.hint_dynamic();
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(format!("-l{name}"));
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
|
2024-06-06 19:57:36 +03:00
|
|
|
fn link_dylib_by_path(&mut self, path: &Path, _as_needed: bool) {
|
|
|
|
self.hint_dynamic();
|
|
|
|
self.link_or_cc_arg(path);
|
|
|
|
}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool, whole_archive: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
self.hint_static();
|
2024-01-18 17:41:18 +03:00
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(format!("-l{name}"));
|
2024-01-18 17:41:18 +03:00
|
|
|
} else {
|
2024-01-18 18:09:59 +03:00
|
|
|
let mut arg = OsString::from("-bkeepfile:");
|
2024-04-12 17:01:46 +03:00
|
|
|
arg.push(find_native_static_library(name, verbatim, self.sess));
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(arg);
|
2024-01-18 17:41:18 +03:00
|
|
|
}
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
self.hint_static();
|
2024-01-18 17:41:18 +03:00
|
|
|
if !whole_archive {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(path);
|
2024-01-18 17:41:18 +03:00
|
|
|
} else {
|
2024-01-18 18:09:59 +03:00
|
|
|
let mut arg = OsString::from("-bkeepfile:");
|
|
|
|
arg.push(path);
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(arg);
|
2024-01-18 17:41:18 +03:00
|
|
|
}
|
2024-01-18 15:47:41 +03:00
|
|
|
}
|
|
|
|
|
2023-03-23 15:00:46 +08:00
|
|
|
fn full_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn partial_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn no_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn gc_sections(&mut self, _keep_metadata: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-bgc");
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn no_gc_sections(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-bnogc");
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn optimize(&mut self) {}
|
|
|
|
|
2024-02-28 17:41:12 +08:00
|
|
|
fn pgo_gen(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-bdbg:namedsects:ss");
|
2024-02-28 17:41:12 +08:00
|
|
|
}
|
2023-03-23 15:00:46 +08:00
|
|
|
|
|
|
|
fn control_flow_guard(&mut self) {}
|
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
2023-10-24 14:59:05 +08:00
|
|
|
fn debuginfo(&mut self, _: Strip, _: &[PathBuf]) {}
|
2023-03-23 15:00:46 +08:00
|
|
|
|
|
|
|
fn no_crt_objects(&mut self) {}
|
|
|
|
|
|
|
|
fn no_default_libraries(&mut self) {}
|
|
|
|
|
|
|
|
fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
|
|
|
|
let path = tmpdir.join("list.exp");
|
|
|
|
let res: io::Result<()> = try {
|
|
|
|
let mut f = BufWriter::new(File::create(&path)?);
|
2023-03-23 11:12:03 +00:00
|
|
|
// FIXME: use llvm-nm to generate export list.
|
2023-03-23 15:00:46 +08:00
|
|
|
for symbol in symbols {
|
2023-07-25 23:04:01 +02:00
|
|
|
debug!(" _{symbol}");
|
|
|
|
writeln!(f, " {symbol}")?;
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Err(e) = res {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.sess.dcx().fatal(format!("failed to write export file: {e}"));
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(format!("-bE:{}", path.to_str().unwrap()));
|
2023-03-23 15:00:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn subsystem(&mut self, _subsystem: &str) {}
|
|
|
|
|
|
|
|
fn reset_per_library_state(&mut self) {
|
|
|
|
self.hint_dynamic();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn linker_plugin_lto(&mut self) {}
|
|
|
|
|
|
|
|
fn add_eh_frame_header(&mut self) {}
|
|
|
|
|
|
|
|
fn add_no_exec(&mut self) {}
|
|
|
|
|
|
|
|
fn add_as_needed(&mut self) {}
|
|
|
|
}
|
|
|
|
|
2022-04-10 02:16:12 +01:00
|
|
|
fn for_each_exported_symbols_include_dep<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
crate_type: CrateType,
|
|
|
|
mut callback: impl FnMut(ExportedSymbol<'tcx>, SymbolExportInfo, CrateNum),
|
|
|
|
) {
|
2022-04-02 22:27:33 +01:00
|
|
|
for &(symbol, info) in tcx.exported_symbols(LOCAL_CRATE).iter() {
|
2022-04-10 02:16:12 +01:00
|
|
|
callback(symbol, info, LOCAL_CRATE);
|
2018-10-23 17:01:35 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 11:26:52 +02:00
|
|
|
let formats = tcx.dependency_formats(());
|
2020-04-24 20:03:45 -07:00
|
|
|
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();
|
2018-10-23 17:01:35 +02:00
|
|
|
|
2019-09-16 13:34:57 -07:00
|
|
|
for (index, dep_format) in deps.iter().enumerate() {
|
2018-10-23 17:01:35 +02:00
|
|
|
let cnum = CrateNum::new(index + 1);
|
|
|
|
// For each dependency that we are linking to statically ...
|
|
|
|
if *dep_format == Linkage::Static {
|
2022-04-02 22:27:33 +01:00
|
|
|
for &(symbol, info) in tcx.exported_symbols(cnum).iter() {
|
2022-04-10 02:16:12 +01:00
|
|
|
callback(symbol, info, cnum);
|
2018-10-23 17:01:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 02:16:12 +01:00
|
|
|
}
|
|
|
|
|
2021-07-06 18:28:07 +02:00
|
|
|
pub(crate) fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
|
2020-11-08 14:27:51 +03:00
|
|
|
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
|
2022-03-22 11:43:05 +01:00
|
|
|
return exports.iter().map(ToString::to_string).collect();
|
2018-11-19 15:04:28 +05:30
|
|
|
}
|
|
|
|
|
2023-07-21 09:41:07 -04:00
|
|
|
if let CrateType::ProcMacro = crate_type {
|
|
|
|
exported_symbols_for_proc_macro_crate(tcx)
|
|
|
|
} else {
|
|
|
|
exported_symbols_for_non_proc_macro(tcx, crate_type)
|
|
|
|
}
|
|
|
|
}
|
2018-10-23 17:01:35 +02:00
|
|
|
|
2023-07-21 09:41:07 -04:00
|
|
|
fn exported_symbols_for_non_proc_macro(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
|
|
|
|
let mut symbols = Vec::new();
|
2018-10-23 17:01:35 +02:00
|
|
|
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
|
2022-04-10 02:16:12 +01:00
|
|
|
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
|
|
|
|
if info.level.is_below_threshold(export_threshold) {
|
2023-11-13 20:48:23 +08:00
|
|
|
symbols.push(symbol_export::exporting_symbol_name_for_instance_in_crate(
|
|
|
|
tcx, symbol, cnum,
|
|
|
|
));
|
2018-10-23 17:01:35 +02:00
|
|
|
}
|
2022-04-10 02:16:12 +01:00
|
|
|
});
|
2018-10-23 17:01:35 +02:00
|
|
|
|
|
|
|
symbols
|
|
|
|
}
|
2019-09-23 14:01:06 -07:00
|
|
|
|
2023-07-21 09:41:07 -04:00
|
|
|
fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<String> {
|
2023-08-07 13:31:14 -04:00
|
|
|
// `exported_symbols` will be empty when !should_codegen.
|
|
|
|
if !tcx.sess.opts.output_types.should_codegen() {
|
|
|
|
return Vec::new();
|
|
|
|
}
|
2023-07-21 09:41:07 -04:00
|
|
|
|
2023-08-08 20:08:24 +08:00
|
|
|
let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
|
2023-07-21 09:41:07 -04:00
|
|
|
let proc_macro_decls_name = tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
|
|
|
|
let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx);
|
|
|
|
|
2023-08-07 13:31:14 -04:00
|
|
|
vec![proc_macro_decls_name, metadata_symbol_name]
|
2023-07-21 09:41:07 -04:00
|
|
|
}
|
|
|
|
|
2022-04-02 22:54:51 +01:00
|
|
|
pub(crate) fn linked_symbols(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
crate_type: CrateType,
|
|
|
|
) -> Vec<(String, SymbolExportKind)> {
|
|
|
|
match crate_type {
|
2022-04-26 23:15:54 +03:00
|
|
|
CrateType::Executable | CrateType::Cdylib | CrateType::Dylib => (),
|
|
|
|
CrateType::Staticlib | CrateType::ProcMacro | CrateType::Rlib => {
|
2022-04-02 22:54:51 +01:00
|
|
|
return Vec::new();
|
2018-10-23 17:01:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 22:54:51 +01:00
|
|
|
let mut symbols = Vec::new();
|
|
|
|
|
|
|
|
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
|
2022-04-10 02:16:12 +01:00
|
|
|
for_each_exported_symbols_include_dep(tcx, crate_type, |symbol, info, cnum| {
|
2022-04-02 22:54:51 +01:00
|
|
|
if info.level.is_below_threshold(export_threshold) || info.used {
|
|
|
|
symbols.push((
|
2022-04-26 18:01:10 +01:00
|
|
|
symbol_export::linking_symbol_name_for_instance_in_crate(tcx, symbol, cnum),
|
2022-04-02 22:54:51 +01:00
|
|
|
info.kind,
|
|
|
|
));
|
|
|
|
}
|
2022-04-10 02:16:12 +01:00
|
|
|
});
|
2022-04-02 22:54:51 +01:00
|
|
|
|
2018-10-23 17:01:35 +02:00
|
|
|
symbols
|
|
|
|
}
|
2019-01-19 21:59:34 +01:00
|
|
|
|
|
|
|
/// Much simplified and explicit CLI for the NVPTX linker. The linker operates
|
|
|
|
/// with bitcode and uses LLVM backend to generate a PTX assembly.
|
|
|
|
pub struct PtxLinker<'a> {
|
|
|
|
cmd: Command,
|
|
|
|
sess: &'a Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for PtxLinker<'a> {
|
2020-04-05 02:11:29 +03:00
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
|
|
|
|
2020-05-02 02:22:48 +03:00
|
|
|
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
panic!("staticlibs not supported")
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--rlib").link_arg(path);
|
2019-01-19 21:59:34 +01:00
|
|
|
}
|
|
|
|
|
2022-04-25 18:02:43 -07:00
|
|
|
fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--debug");
|
2019-01-19 21:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_object(&mut self, path: &Path) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--bitcode").link_arg(path);
|
2019-01-19 21:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn optimize(&mut self) {
|
2019-01-28 23:56:37 +01:00
|
|
|
match self.sess.lto() {
|
|
|
|
Lto::Thin | Lto::Fat | Lto::ThinLocal => {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("-Olto");
|
2019-01-28 23:56:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Lto::No => {}
|
2024-02-06 19:46:12 +01:00
|
|
|
}
|
2019-01-19 21:59:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn full_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn partial_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn no_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn gc_sections(&mut self, _keep_metadata: bool) {}
|
|
|
|
|
2021-03-24 21:45:09 -07:00
|
|
|
fn no_gc_sections(&mut self) {}
|
|
|
|
|
2019-01-19 21:59:34 +01:00
|
|
|
fn pgo_gen(&mut self) {}
|
|
|
|
|
2020-04-29 20:47:07 +03:00
|
|
|
fn no_crt_objects(&mut self) {}
|
|
|
|
|
2019-01-19 21:59:34 +01:00
|
|
|
fn no_default_libraries(&mut self) {}
|
|
|
|
|
2020-07-06 16:10:42 +01:00
|
|
|
fn control_flow_guard(&mut self) {}
|
2020-01-13 13:25:39 +00:00
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
2021-07-06 18:28:07 +02:00
|
|
|
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, _symbols: &[String]) {}
|
2019-01-19 21:59:34 +01:00
|
|
|
|
|
|
|
fn subsystem(&mut self, _subsystem: &str) {}
|
|
|
|
|
2019-02-12 17:17:05 +01:00
|
|
|
fn linker_plugin_lto(&mut self) {}
|
2019-01-19 21:59:34 +01:00
|
|
|
}
|
2020-11-30 19:41:57 +00:00
|
|
|
|
2024-02-06 19:46:12 +01:00
|
|
|
/// The `self-contained` LLVM bitcode linker
|
|
|
|
pub struct LlbcLinker<'a> {
|
|
|
|
cmd: Command,
|
|
|
|
sess: &'a Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for LlbcLinker<'a> {
|
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
|
2024-02-06 19:46:12 +01:00
|
|
|
panic!("staticlibs not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(path);
|
2024-02-06 19:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--debug");
|
2024-02-06 19:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn optimize(&mut self) {
|
|
|
|
match self.sess.opts.optimize {
|
|
|
|
OptLevel::No => "-O0",
|
|
|
|
OptLevel::Less => "-O1",
|
|
|
|
OptLevel::Default => "-O2",
|
|
|
|
OptLevel::Aggressive => "-O3",
|
|
|
|
OptLevel::Size => "-Os",
|
|
|
|
OptLevel::SizeMin => "-Oz",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn partial_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn no_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn gc_sections(&mut self, _keep_metadata: bool) {}
|
|
|
|
|
|
|
|
fn no_gc_sections(&mut self) {}
|
|
|
|
|
|
|
|
fn pgo_gen(&mut self) {}
|
|
|
|
|
|
|
|
fn no_crt_objects(&mut self) {}
|
|
|
|
|
|
|
|
fn no_default_libraries(&mut self) {}
|
|
|
|
|
|
|
|
fn control_flow_guard(&mut self) {}
|
|
|
|
|
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
|
|
|
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
|
|
|
|
match _crate_type {
|
|
|
|
CrateType::Cdylib => {
|
|
|
|
for sym in symbols {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_args(&["--export-symbol", sym]);
|
2024-02-06 19:46:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subsystem(&mut self, _subsystem: &str) {}
|
|
|
|
|
|
|
|
fn linker_plugin_lto(&mut self) {}
|
|
|
|
}
|
|
|
|
|
2020-11-30 19:41:57 +00:00
|
|
|
pub struct BpfLinker<'a> {
|
|
|
|
cmd: Command,
|
|
|
|
sess: &'a Session,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Linker for BpfLinker<'a> {
|
|
|
|
fn cmd(&mut self) -> &mut Command {
|
|
|
|
&mut self.cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
|
|
|
|
|
2024-04-12 17:01:46 +03:00
|
|
|
fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool, _whole_archive: bool) {
|
2024-01-18 15:47:41 +03:00
|
|
|
panic!("staticlibs not supported")
|
|
|
|
}
|
|
|
|
|
2024-01-18 17:41:18 +03:00
|
|
|
fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_or_cc_arg(path);
|
2020-11-30 19:41:57 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 18:02:43 -07:00
|
|
|
fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--debug");
|
2020-11-30 19:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn optimize(&mut self) {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg(match self.sess.opts.optimize {
|
2020-11-30 19:41:57 +00:00
|
|
|
OptLevel::No => "-O0",
|
|
|
|
OptLevel::Less => "-O1",
|
|
|
|
OptLevel::Default => "-O2",
|
|
|
|
OptLevel::Aggressive => "-O3",
|
|
|
|
OptLevel::Size => "-Os",
|
|
|
|
OptLevel::SizeMin => "-Oz",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn partial_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn no_relro(&mut self) {}
|
|
|
|
|
|
|
|
fn gc_sections(&mut self, _keep_metadata: bool) {}
|
|
|
|
|
|
|
|
fn no_gc_sections(&mut self) {}
|
|
|
|
|
|
|
|
fn pgo_gen(&mut self) {}
|
|
|
|
|
|
|
|
fn no_crt_objects(&mut self) {}
|
|
|
|
|
|
|
|
fn no_default_libraries(&mut self) {}
|
|
|
|
|
|
|
|
fn control_flow_guard(&mut self) {}
|
|
|
|
|
2023-11-17 10:05:38 -08:00
|
|
|
fn ehcont_guard(&mut self) {}
|
|
|
|
|
2021-07-06 18:28:07 +02:00
|
|
|
fn export_symbols(&mut self, tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) {
|
2020-11-30 19:41:57 +00:00
|
|
|
let path = tmpdir.join("symbols");
|
|
|
|
let res: io::Result<()> = try {
|
|
|
|
let mut f = BufWriter::new(File::create(&path)?);
|
2021-07-06 18:28:07 +02:00
|
|
|
for sym in symbols {
|
2023-07-25 23:04:01 +02:00
|
|
|
writeln!(f, "{sym}")?;
|
2020-11-30 19:41:57 +00:00
|
|
|
}
|
|
|
|
};
|
2022-10-07 10:03:45 -04:00
|
|
|
if let Err(error) = res {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.sess.dcx().emit_fatal(errors::SymbolFileWriteFailure { error });
|
2020-11-30 19:41:57 +00:00
|
|
|
} else {
|
2024-01-27 19:35:55 +03:00
|
|
|
self.link_arg("--export-symbols").link_arg(&path);
|
2020-11-30 19:41:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn subsystem(&mut self, _subsystem: &str) {}
|
|
|
|
|
|
|
|
fn linker_plugin_lto(&mut self) {}
|
|
|
|
}
|