use super::archive;
use super::command::Command;
use super::symbol_export;
use rustc_span::symbol::sym;
use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
use std::io::prelude::*;
use std::io::{self, BufWriter};
use std::mem;
use std::path::{Path, PathBuf};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE};
use rustc_middle::middle::dependency_format::Linkage;
use rustc_middle::ty::TyCtxt;
use rustc_serialize::{json, Encoder};
use rustc_session::config::{self, CrateType, DebugInfo, LinkerPluginLto, Lto, OptLevel, Strip};
use rustc_session::Session;
use rustc_span::symbol::Symbol;
use rustc_target::spec::{LinkOutputKind, LinkerFlavor, LldFlavor};
pub fn disable_localization(linker: &mut Command) {
linker.env("LC_ALL", "C");
linker.env("VSLANG", "1033");
}
#[derive(Encodable, Decodable)]
pub struct LinkerInfo {
exports: FxHashMap<CrateType, Vec<String>>,
}
impl LinkerInfo {
pub fn new(tcx: TyCtxt<'_>) -> LinkerInfo {
LinkerInfo {
exports: tcx
.sess
.crate_types()
.iter()
.map(|&c| (c, exported_symbols(tcx, c)))
.collect(),
}
}
pub fn to_linker<'a>(
&'a self,
cmd: Command,
sess: &'a Session,
flavor: LinkerFlavor,
target_cpu: &'a str,
) -> Box<dyn Linker + 'a> {
match flavor {
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => {
Box::new(MsvcLinker { cmd, sess, info: self }) as Box<dyn Linker>
}
LinkerFlavor::Em => Box::new(EmLinker { cmd, sess, info: self }) as Box<dyn Linker>,
LinkerFlavor::Gcc => Box::new(GccLinker {
cmd,
sess,
info: self,
hinted_static: false,
is_ld: false,
target_cpu,
}) as Box<dyn Linker>,
LinkerFlavor::Lld(LldFlavor::Ld)
| LinkerFlavor::Lld(LldFlavor::Ld64)
| LinkerFlavor::Ld => Box::new(GccLinker {
cmd,
sess,
info: self,
hinted_static: false,
is_ld: true,
target_cpu,
}) as Box<dyn Linker>,
LinkerFlavor::Lld(LldFlavor::Wasm) => {
Box::new(WasmLd::new(cmd, sess, self)) as Box<dyn Linker>
}
LinkerFlavor::PtxLinker => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
}
}
}
pub trait Linker {
fn cmd(&mut self) -> &mut Command;
fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path);
fn link_dylib(&mut self, lib: Symbol);
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path);
fn link_framework(&mut self, framework: Symbol);
fn link_staticlib(&mut self, lib: Symbol);
fn link_rlib(&mut self, lib: &Path);
fn link_whole_rlib(&mut self, lib: &Path);
fn link_whole_staticlib(&mut self, lib: Symbol, search_path: &[PathBuf]);
fn include_path(&mut self, path: &Path);
fn framework_path(&mut self, path: &Path);
fn output_filename(&mut self, path: &Path);
fn add_object(&mut self, path: &Path);
fn gc_sections(&mut self, keep_metadata: bool);
fn full_relro(&mut self);
fn partial_relro(&mut self);
fn no_relro(&mut self);
fn optimize(&mut self);
fn pgo_gen(&mut self);
fn control_flow_guard(&mut self);
fn debuginfo(&mut self, strip: Strip);
fn no_crt_objects(&mut self);
fn no_default_libraries(&mut self);
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
fn subsystem(&mut self, subsystem: &str);
fn group_start(&mut self);
fn group_end(&mut self);
fn linker_plugin_lto(&mut self);
fn add_eh_frame_header(&mut self) {}
fn finalize(&mut self);
}
impl dyn Linker + '_ {
pub fn arg(&mut self, arg: impl AsRef<OsStr>) {
self.cmd().arg(arg);
}
pub fn args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) {
self.cmd().args(args);
}
pub fn take_cmd(&mut self) -> Command {
mem::replace(self.cmd(), Command::new(""))
}
}
pub struct GccLinker<'a> {
cmd: Command,
sess: &'a Session,
info: &'a LinkerInfo,
hinted_static: bool,
is_ld: bool,
target_cpu: &'a str,
}
impl<'a> GccLinker<'a> {
fn linker_arg<S>(&mut self, arg: S) -> &mut Self
where
S: AsRef<OsStr>,
{
if !self.is_ld {
let mut os = OsString::from("-Wl,");
os.push(arg.as_ref());
self.cmd.arg(os);
} else {
self.cmd.arg(arg);
}
self
}
fn takes_hints(&self) -> bool {
!self.sess.target.is_like_osx && self.sess.target.arch != "wasm32"
}
fn hint_static(&mut self) {
if !self.takes_hints() {
return;
}
if !self.hinted_static {
self.linker_arg("-Bstatic");
self.hinted_static = true;
}
}
fn hint_dynamic(&mut self) {
if !self.takes_hints() {
return;
}
if self.hinted_static {
self.linker_arg("-Bdynamic");
self.hinted_static = false;
}
}
fn push_linker_plugin_lto_args(&mut self, plugin_path: Option<&OsStr>) {
if let Some(plugin_path) = plugin_path {
let mut arg = OsString::from("-plugin=");
arg.push(plugin_path);
self.linker_arg(&arg);
}
let opt_level = match self.sess.opts.optimize {
config::OptLevel::No => "O0",
config::OptLevel::Less => "O1",
config::OptLevel::Default | config::OptLevel::Size | config::OptLevel::SizeMin => "O2",
config::OptLevel::Aggressive => "O3",
};
self.linker_arg(&format!("-plugin-opt={}", opt_level));
let target_cpu = self.target_cpu;
self.linker_arg(&format!("-plugin-opt=mcpu={}", target_cpu));
}
fn build_dylib(&mut self, out_filename: &Path) {
if self.sess.target.is_like_osx {
self.cmd.arg("-dynamiclib");
self.linker_arg("-dylib");
if self.sess.opts.cg.rpath || self.sess.opts.debugging_opts.osx_rpath_install_name {
self.linker_arg("-install_name");
let mut v = OsString::from("@rpath/");
v.push(out_filename.file_name().unwrap());
self.linker_arg(&v);
}
} else {
self.cmd.arg("-shared");
if self.sess.target.is_like_windows {
let implib_name =
out_filename.file_name().and_then(|file| file.to_str()).map(|file| {
format!(
"{}{}{}",
self.sess.target.staticlib_prefix,
file,
self.sess.target.staticlib_suffix
)
});
if let Some(implib_name) = implib_name {
let implib = out_filename.parent().map(|dir| dir.join(&implib_name));
if let Some(implib) = implib {
self.linker_arg(&format!("--out-implib={}", (*implib).to_str().unwrap()));
}
}
}
}
}
}
impl<'a> Linker for GccLinker<'a> {
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::DynamicNoPicExe => {
if !self.is_ld && self.sess.target.linker_is_gnu {
self.cmd.arg("-no-pie");
}
}
LinkOutputKind::DynamicPicExe => {
self.cmd.arg("-pie");
}
LinkOutputKind::StaticNoPicExe => {
self.cmd.arg("-static");
if !self.is_ld && self.sess.target.linker_is_gnu {
self.cmd.arg("-no-pie");
}
}
LinkOutputKind::StaticPicExe => {
if !self.is_ld {
self.cmd.arg("-static-pie");
} else {
self.cmd.args(&["-static", "-pie", "--no-dynamic-linker", "-z", "text"]);
}
}
LinkOutputKind::DynamicDylib => self.build_dylib(out_filename),
LinkOutputKind::StaticDylib => {
self.cmd.arg("-static");
self.build_dylib(out_filename);
}
}
if self.sess.target.os == "vxworks"
&& matches!(
output_kind,
LinkOutputKind::StaticNoPicExe
| LinkOutputKind::StaticPicExe
| LinkOutputKind::StaticDylib
)
{
self.cmd.arg("--static-crt");
}
}
fn link_dylib(&mut self, lib: Symbol) {
self.hint_dynamic();
self.cmd.arg(format!("-l{}", lib));
}
fn link_staticlib(&mut self, lib: Symbol) {
self.hint_static();
self.cmd.arg(format!("-l{}", lib));
}
fn link_rlib(&mut self, lib: &Path) {
self.hint_static();
self.cmd.arg(lib);
}
fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path);
}
fn framework_path(&mut self, path: &Path) {
self.cmd.arg("-F").arg(path);
}
fn output_filename(&mut self, path: &Path) {
self.cmd.arg("-o").arg(path);
}
fn add_object(&mut self, path: &Path) {
self.cmd.arg(path);
}
fn full_relro(&mut self) {
self.linker_arg("-zrelro");
self.linker_arg("-znow");
}
fn partial_relro(&mut self) {
self.linker_arg("-zrelro");
}
fn no_relro(&mut self) {
self.linker_arg("-znorelro");
}
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
self.hint_dynamic();
self.cmd.arg(format!("-l{}", lib));
}
fn link_framework(&mut self, framework: Symbol) {
self.hint_dynamic();
self.cmd.arg("-framework").sym_arg(framework);
}
fn link_whole_staticlib(&mut self, lib: Symbol, search_path: &[PathBuf]) {
self.hint_static();
let target = &self.sess.target;
if !target.is_like_osx {
self.linker_arg("--whole-archive").cmd.arg(format!("-l{}", lib));
self.linker_arg("--no-whole-archive");
} else {
self.linker_arg("-force_load");
let lib = archive::find_library(lib, search_path, &self.sess);
self.linker_arg(&lib);
}
}
fn link_whole_rlib(&mut self, lib: &Path) {
self.hint_static();
if self.sess.target.is_like_osx {
self.linker_arg("-force_load");
self.linker_arg(&lib);
} else {
self.linker_arg("--whole-archive").cmd.arg(lib);
self.linker_arg("--no-whole-archive");
}
}
fn gc_sections(&mut self, keep_metadata: bool) {
if self.sess.target.is_like_osx {
self.linker_arg("-dead_strip");
} else if self.sess.target.is_like_solaris {
self.linker_arg("-zignore");
} else if !keep_metadata {
self.linker_arg("--gc-sections");
}
}
fn optimize(&mut self) {
if !self.sess.target.linker_is_gnu {
return;
}
if self.sess.opts.optimize == config::OptLevel::Default
|| self.sess.opts.optimize == config::OptLevel::Aggressive
{
self.linker_arg("-O1");
}
}
fn pgo_gen(&mut self) {
if !self.sess.target.linker_is_gnu {
return;
}
self.cmd.arg("-u");
self.cmd.arg("__llvm_profile_runtime");
}
fn control_flow_guard(&mut self) {}
fn debuginfo(&mut self, strip: Strip) {
match strip {
Strip::None => {}
Strip::Debuginfo => {
self.linker_arg("-S");
}
Strip::Symbols => {
self.linker_arg("-s");
}
}
}
fn no_crt_objects(&mut self) {
if !self.is_ld {
self.cmd.arg("-nostartfiles");
}
}
fn no_default_libraries(&mut self) {
if !self.is_ld {
self.cmd.arg("-nodefaultlibs");
}
}
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
if crate_type == CrateType::Executable && self.sess.target.override_export_symbols.is_none()
{
return;
}
if !self.sess.target.limit_rdylib_exports {
return;
}
if crate_type == CrateType::ProcMacro {
return;
}
let is_windows = self.sess.target.is_like_windows;
let mut arg = OsString::new();
let path = tmpdir.join(if is_windows { "list.def" } else { "list" });
debug!("EXPORTED SYMBOLS:");
if self.sess.target.is_like_osx {
let res: io::Result<()> = try {
let mut f = BufWriter::new(File::create(&path)?);
for sym in self.info.exports[&crate_type].iter() {
debug!(" _{}", sym);
writeln!(f, "_{}", sym)?;
}
};
if let Err(e) = res {
self.sess.fatal(&format!("failed to write lib.def file: {}", e));
}
} else if is_windows {
let res: io::Result<()> = try {
let mut f = BufWriter::new(File::create(&path)?);
writeln!(f, "EXPORTS")?;
for symbol in self.info.exports[&crate_type].iter() {
debug!(" _{}", symbol);
writeln!(f, " {}", symbol)?;
}
};
if let Err(e) = res {
self.sess.fatal(&format!("failed to write list.def file: {}", e));
}
} else {
let res: io::Result<()> = try {
let mut f = BufWriter::new(File::create(&path)?);
writeln!(f, "{{")?;
if !self.info.exports[&crate_type].is_empty() {
writeln!(f, " global:")?;
for sym in self.info.exports[&crate_type].iter() {
debug!(" {};", sym);
writeln!(f, " {};", sym)?;
}
}
writeln!(f, "\n local:\n *;\n}};")?;
};
if let Err(e) = res {
self.sess.fatal(&format!("failed to write version script: {}", e));
}
}
if self.sess.target.is_like_osx {
if !self.is_ld {
arg.push("-Wl,")
}
arg.push("-exported_symbols_list,");
} else if self.sess.target.is_like_solaris {
if !self.is_ld {
arg.push("-Wl,")
}
arg.push("-M,");
} else {
if !self.is_ld {
arg.push("-Wl,")
}
if !is_windows {
arg.push("--version-script=")
}
}
arg.push(&path);
self.cmd.arg(arg);
}
fn subsystem(&mut self, subsystem: &str) {
self.linker_arg("--subsystem");
self.linker_arg(&subsystem);
}
fn finalize(&mut self) {
self.hint_dynamic();
}
fn group_start(&mut self) {
if self.takes_hints() {
self.linker_arg("--start-group");
}
}
fn group_end(&mut self) {
if self.takes_hints() {
self.linker_arg("--end-group");
}
}
fn linker_plugin_lto(&mut self) {
match self.sess.opts.cg.linker_plugin_lto {
LinkerPluginLto::Disabled => {
}
LinkerPluginLto::LinkerPluginAuto => {
self.push_linker_plugin_lto_args(None);
}
LinkerPluginLto::LinkerPlugin(ref path) => {
self.push_linker_plugin_lto_args(Some(path.as_os_str()));
}
}
}
fn add_eh_frame_header(&mut self) {
self.linker_arg("--eh-frame-hdr");
}
}
pub struct MsvcLinker<'a> {
cmd: Command,
sess: &'a Session,
info: &'a LinkerInfo,
}
impl<'a> Linker for MsvcLinker<'a> {
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::DynamicNoPicExe
| LinkOutputKind::DynamicPicExe
| LinkOutputKind::StaticNoPicExe
| LinkOutputKind::StaticPicExe => {}
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
self.cmd.arg("/DLL");
let mut arg: OsString = "/IMPLIB:".into();
arg.push(out_filename.with_extension("dll.lib"));
self.cmd.arg(arg);
}
}
}
fn link_rlib(&mut self, lib: &Path) {
self.cmd.arg(lib);
}
fn add_object(&mut self, path: &Path) {
self.cmd.arg(path);
}
fn gc_sections(&mut self, _keep_metadata: bool) {
if self.sess.opts.optimize != config::OptLevel::No {
self.cmd.arg("/OPT:REF,ICF");
} else {
self.cmd.arg("/OPT:REF,NOICF");
}
}
fn link_dylib(&mut self, lib: Symbol) {
self.cmd.arg(&format!("{}.lib", lib));
}
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path) {
let name = format!("{}.dll.lib", lib);
if fs::metadata(&path.join(&name)).is_ok() {
self.cmd.arg(name);
}
}
fn link_staticlib(&mut self, lib: Symbol) {
self.cmd.arg(&format!("{}.lib", lib));
}
fn full_relro(&mut self) {
}
fn partial_relro(&mut self) {
}
fn no_relro(&mut self) {
}
fn no_crt_objects(&mut self) {
}
fn no_default_libraries(&mut self) {
self.cmd.arg("/NODEFAULTLIB");
}
fn include_path(&mut self, path: &Path) {
let mut arg = OsString::from("/LIBPATH:");
arg.push(path);
self.cmd.arg(&arg);
}
fn output_filename(&mut self, path: &Path) {
let mut arg = OsString::from("/OUT:");
arg.push(path);
self.cmd.arg(&arg);
}
fn framework_path(&mut self, _path: &Path) {
bug!("frameworks are not supported on windows")
}
fn link_framework(&mut self, _framework: Symbol) {
bug!("frameworks are not supported on windows")
}
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
self.link_staticlib(lib);
self.cmd.arg(format!("/WHOLEARCHIVE:{}.lib", lib));
}
fn link_whole_rlib(&mut self, path: &Path) {
self.link_rlib(path);
let mut arg = OsString::from("/WHOLEARCHIVE:");
arg.push(path);
self.cmd.arg(arg);
}
fn optimize(&mut self) {
}
fn pgo_gen(&mut self) {
}
fn control_flow_guard(&mut self) {
self.cmd.arg("/guard:cf");
}
fn debuginfo(&mut self, strip: Strip) {
match strip {
Strip::None => {
self.cmd.arg("/DEBUG");
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);
self.cmd.arg(arg);
}
}
Err(err) => {
self.sess
.warn(&format!("error enumerating natvis directory: {}", err));
}
}
}
}
}
Strip::Debuginfo | Strip::Symbols => {
self.cmd.arg("/DEBUG:NONE");
}
}
}
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType) {
if crate_type == CrateType::Executable {
return;
}
let path = tmpdir.join("lib.def");
let res: io::Result<()> = try {
let mut f = BufWriter::new(File::create(&path)?);
writeln!(f, "LIBRARY")?;
writeln!(f, "EXPORTS")?;
for symbol in self.info.exports[&crate_type].iter() {
debug!(" _{}", symbol);
writeln!(f, " {}", symbol)?;
}
};
if let Err(e) = res {
self.sess.fatal(&format!("failed to write lib.def file: {}", e));
}
let mut arg = OsString::from("/DEF:");
arg.push(path);
self.cmd.arg(&arg);
}
fn subsystem(&mut self, subsystem: &str) {
self.cmd.arg(&format!("/SUBSYSTEM:{}", subsystem));
if subsystem == "windows" {
self.cmd.arg("/ENTRY:mainCRTStartup");
}
}
fn finalize(&mut self) {}
fn group_start(&mut self) {}
fn group_end(&mut self) {}
fn linker_plugin_lto(&mut self) {
}
}
pub struct EmLinker<'a> {
cmd: Command,
sess: &'a Session,
info: &'a LinkerInfo,
}
impl<'a> Linker for EmLinker<'a> {
fn cmd(&mut self) -> &mut Command {
&mut self.cmd
}
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path);
}
fn link_staticlib(&mut self, lib: Symbol) {
self.cmd.arg("-l").sym_arg(lib);
}
fn output_filename(&mut self, path: &Path) {
self.cmd.arg("-o").arg(path);
}
fn add_object(&mut self, path: &Path) {
self.cmd.arg(path);
}
fn link_dylib(&mut self, lib: Symbol) {
self.link_staticlib(lib);
}
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
self.link_staticlib(lib);
}
fn link_whole_rlib(&mut self, lib: &Path) {
self.link_rlib(lib);
}
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
self.link_dylib(lib);
}
fn link_rlib(&mut self, lib: &Path) {
self.add_object(lib);
}
fn full_relro(&mut self) {
}
fn partial_relro(&mut self) {
}
fn no_relro(&mut self) {
}
fn framework_path(&mut self, _path: &Path) {
bug!("frameworks are not supported on Emscripten")
}
fn link_framework(&mut self, _framework: Symbol) {
bug!("frameworks are not supported on Emscripten")
}
fn gc_sections(&mut self, _keep_metadata: bool) {
}
fn optimize(&mut self) {
self.cmd.arg(match self.sess.opts.optimize {
OptLevel::No => "-O0",
OptLevel::Less => "-O1",
OptLevel::Default => "-O2",
OptLevel::Aggressive => "-O3",
OptLevel::Size => "-Os",
OptLevel::SizeMin => "-Oz",
});
self.cmd.args(&["--memory-init-file", "0"]);
}
fn pgo_gen(&mut self) {
}
fn control_flow_guard(&mut self) {}
fn debuginfo(&mut self, _strip: Strip) {
self.cmd.arg(match self.sess.opts.debuginfo {
DebugInfo::None => "-g0",
DebugInfo::Limited => "-g3",
DebugInfo::Full => "-g4",
});
}
fn no_crt_objects(&mut self) {}
fn no_default_libraries(&mut self) {
self.cmd.args(&["-s", "DEFAULT_LIBRARY_FUNCS_TO_INCLUDE=[]"]);
}
fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
let symbols = &self.info.exports[&crate_type];
debug!("EXPORTED SYMBOLS:");
self.cmd.arg("-s");
let mut arg = OsString::from("EXPORTED_FUNCTIONS=");
let mut encoded = String::new();
{
let mut encoder = json::Encoder::new(&mut encoded);
let res = encoder.emit_seq(symbols.len(), |encoder| {
for (i, sym) in symbols.iter().enumerate() {
encoder.emit_seq_elt(i, |encoder| encoder.emit_str(&("_".to_owned() + sym)))?;
}
Ok(())
});
if let Err(e) = res {
self.sess.fatal(&format!("failed to encode exported symbols: {}", e));
}
}
debug!("{}", encoded);
arg.push(encoded);
self.cmd.arg(arg);
}
fn subsystem(&mut self, _subsystem: &str) {
}
fn finalize(&mut self) {}
fn group_start(&mut self) {}
fn group_end(&mut self) {}
fn linker_plugin_lto(&mut self) {
}
}
pub struct WasmLd<'a> {
cmd: Command,
sess: &'a Session,
info: &'a LinkerInfo,
}
impl<'a> WasmLd<'a> {
fn new(mut cmd: Command, sess: &'a Session, info: &'a LinkerInfo) -> WasmLd<'a> {
if sess.target_features.contains(&sym::atomics) {
cmd.arg("--shared-memory");
cmd.arg("--max-memory=1073741824");
cmd.arg("--import-memory");
cmd.arg("--export=__wasm_init_memory");
cmd.arg("--export=__wasm_init_tls");
cmd.arg("--export=__tls_size");
cmd.arg("--export=__tls_align");
cmd.arg("--export=__tls_base");
}
WasmLd { cmd, sess, info }
}
}
impl<'a> Linker for WasmLd<'a> {
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::DynamicNoPicExe
| LinkOutputKind::DynamicPicExe
| LinkOutputKind::StaticNoPicExe
| LinkOutputKind::StaticPicExe => {}
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
self.cmd.arg("--no-entry");
}
}
}
fn link_dylib(&mut self, lib: Symbol) {
self.cmd.arg("-l").sym_arg(lib);
}
fn link_staticlib(&mut self, lib: Symbol) {
self.cmd.arg("-l").sym_arg(lib);
}
fn link_rlib(&mut self, lib: &Path) {
self.cmd.arg(lib);
}
fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path);
}
fn framework_path(&mut self, _path: &Path) {
panic!("frameworks not supported")
}
fn output_filename(&mut self, path: &Path) {
self.cmd.arg("-o").arg(path);
}
fn add_object(&mut self, path: &Path) {
self.cmd.arg(path);
}
fn full_relro(&mut self) {}
fn partial_relro(&mut self) {}
fn no_relro(&mut self) {}
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
self.cmd.arg("-l").sym_arg(lib);
}
fn link_framework(&mut self, _framework: Symbol) {
panic!("frameworks not supported")
}
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
self.cmd.arg("-l").sym_arg(lib);
}
fn link_whole_rlib(&mut self, lib: &Path) {
self.cmd.arg(lib);
}
fn gc_sections(&mut self, _keep_metadata: bool) {
self.cmd.arg("--gc-sections");
}
fn optimize(&mut self) {
self.cmd.arg(match self.sess.opts.optimize {
OptLevel::No => "-O0",
OptLevel::Less => "-O1",
OptLevel::Default => "-O2",
OptLevel::Aggressive => "-O3",
OptLevel::Size => "-O2",
OptLevel::SizeMin => "-O2",
});
}
fn pgo_gen(&mut self) {}
fn debuginfo(&mut self, strip: Strip) {
match strip {
Strip::None => {}
Strip::Debuginfo => {
self.cmd.arg("--strip-debug");
}
Strip::Symbols => {
self.cmd.arg("--strip-all");
}
}
}
fn control_flow_guard(&mut self) {}
fn no_crt_objects(&mut self) {}
fn no_default_libraries(&mut self) {}
fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
for sym in self.info.exports[&crate_type].iter() {
self.cmd.arg("--export").arg(&sym);
}
self.cmd.arg("--export=__heap_base");
self.cmd.arg("--export=__data_end");
}
fn subsystem(&mut self, _subsystem: &str) {}
fn finalize(&mut self) {}
fn group_start(&mut self) {}
fn group_end(&mut self) {}
fn linker_plugin_lto(&mut self) {
}
}
fn exported_symbols(tcx: TyCtxt<'_>, crate_type: CrateType) -> Vec<String> {
if let Some(ref exports) = tcx.sess.target.override_export_symbols {
return exports.clone();
}
let mut symbols = Vec::new();
let export_threshold = symbol_export::crates_export_threshold(&[crate_type]);
for &(symbol, level) in tcx.exported_symbols(LOCAL_CRATE).iter() {
if level.is_below_threshold(export_threshold) {
symbols.push(symbol_export::symbol_name_for_instance_in_crate(
tcx,
symbol,
LOCAL_CRATE,
));
}
}
let formats = tcx.dependency_formats(LOCAL_CRATE);
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();
for (index, dep_format) in deps.iter().enumerate() {
let cnum = CrateNum::new(index + 1);
if *dep_format == Linkage::Static {
for &(symbol, level) in tcx.exported_symbols(cnum).iter() {
if !level.is_below_threshold(export_threshold) {
continue;
}
symbols.push(symbol_export::symbol_name_for_instance_in_crate(tcx, symbol, cnum));
}
}
}
symbols
}
pub struct PtxLinker<'a> {
cmd: Command,
sess: &'a Session,
}
impl<'a> Linker for PtxLinker<'a> {
fn cmd(&mut self) -> &mut Command {
&mut self.cmd
}
fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
fn link_rlib(&mut self, path: &Path) {
self.cmd.arg("--rlib").arg(path);
}
fn link_whole_rlib(&mut self, path: &Path) {
self.cmd.arg("--rlib").arg(path);
}
fn include_path(&mut self, path: &Path) {
self.cmd.arg("-L").arg(path);
}
fn debuginfo(&mut self, _strip: Strip) {
self.cmd.arg("--debug");
}
fn add_object(&mut self, path: &Path) {
self.cmd.arg("--bitcode").arg(path);
}
fn optimize(&mut self) {
match self.sess.lto() {
Lto::Thin | Lto::Fat | Lto::ThinLocal => {
self.cmd.arg("-Olto");
}
Lto::No => {}
};
}
fn output_filename(&mut self, path: &Path) {
self.cmd.arg("-o").arg(path);
}
fn finalize(&mut self) {
self.cmd.arg("--fallback-arch").arg(match self.sess.opts.cg.target_cpu {
Some(ref s) => s,
None => &self.sess.target.cpu,
});
}
fn link_dylib(&mut self, _lib: Symbol) {
panic!("external dylibs not supported")
}
fn link_rust_dylib(&mut self, _lib: Symbol, _path: &Path) {
panic!("external dylibs not supported")
}
fn link_staticlib(&mut self, _lib: Symbol) {
panic!("staticlibs not supported")
}
fn link_whole_staticlib(&mut self, _lib: Symbol, _search_path: &[PathBuf]) {
panic!("staticlibs not supported")
}
fn framework_path(&mut self, _path: &Path) {
panic!("frameworks not supported")
}
fn link_framework(&mut self, _framework: Symbol) {
panic!("frameworks not supported")
}
fn full_relro(&mut self) {}
fn partial_relro(&mut self) {}
fn no_relro(&mut self) {}
fn gc_sections(&mut self, _keep_metadata: bool) {}
fn pgo_gen(&mut self) {}
fn no_crt_objects(&mut self) {}
fn no_default_libraries(&mut self) {}
fn control_flow_guard(&mut self) {}
fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType) {}
fn subsystem(&mut self, _subsystem: &str) {}
fn group_start(&mut self) {}
fn group_end(&mut self) {}
fn linker_plugin_lto(&mut self) {}
}