Use Symbol in Linker.

This commit is contained in:
Nicholas Nethercote 2019-09-05 11:23:45 +10:00
parent 5a57f46cbc
commit 482d63673c
5 changed files with 65 additions and 52 deletions

View file

@ -12,6 +12,7 @@ use crate::llvm::{self, ArchiveKind};
use rustc_codegen_ssa::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION};
use rustc_codegen_ssa::back::archive::{ArchiveBuilder, find_library};
use rustc::session::Session;
use syntax::symbol::Symbol;
struct ArchiveConfig<'a> {
pub sess: &'a Session,
@ -109,7 +110,7 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
/// Adds all of the contents of a native library to this archive. This will
/// search in the relevant locations for a library named `name`.
fn add_native_library(&mut self, name: &str) {
fn add_native_library(&mut self, name: Symbol) {
let location = find_library(name, &self.config.lib_search_paths,
self.config.sess);
self.add_archive(&location, |_| false).unwrap_or_else(|e| {

View file

@ -1,9 +1,10 @@
use rustc::session::Session;
use syntax::symbol::Symbol;
use std::io;
use std::path::{Path, PathBuf};
pub fn find_library(name: &str, search_paths: &[PathBuf], sess: &Session)
pub fn find_library(name: Symbol, search_paths: &[PathBuf], sess: &Session)
-> PathBuf {
// On Windows, static libraries sometimes show up as libfoo.a and other
// times show up as foo.lib
@ -40,7 +41,7 @@ pub trait ArchiveBuilder<'a> {
lto: bool,
skip_objects: bool,
) -> io::Result<()>;
fn add_native_library(&mut self, name: &str);
fn add_native_library(&mut self, name: Symbol);
fn update_symbols(&mut self);
fn build(self);

View file

@ -8,6 +8,7 @@ use std::mem;
use std::process::{self, Output};
use rustc_target::spec::LldFlavor;
use syntax::symbol::Symbol;
#[derive(Clone)]
pub struct Command {
@ -49,6 +50,11 @@ impl Command {
self
}
pub fn sym_arg(&mut self, arg: Symbol) -> &mut Command {
self.arg(&arg.as_str());
self
}
pub fn args<I>(&mut self, args: I) -> &mut Command
where
I: IntoIterator<Item: AsRef<OsStr>>,

View file

@ -13,6 +13,7 @@ use rustc::hir::def_id::CrateNum;
use rustc_data_structures::fx::FxHashSet;
use rustc_fs_util::fix_windows_verbatim_for_gcc;
use rustc_target::spec::{PanicStrategy, RelroLevel, LinkerFlavor};
use syntax::symbol::Symbol;
use crate::{METADATA_FILENAME, RLIB_BYTECODE_EXTENSION, CrateInfo, CodegenResults};
use super::archive::ArchiveBuilder;
@ -316,7 +317,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(sess: &'a Session,
NativeLibraryKind::NativeUnknown => continue,
}
if let Some(name) = lib.name {
ab.add_native_library(&name.as_str());
ab.add_native_library(name);
}
}
@ -1273,15 +1274,14 @@ pub fn add_local_native_libraries(cmd: &mut dyn Linker,
let search_path = archive_search_paths(sess);
for lib in relevant_libs {
let name = match lib.name {
Some(ref l) => l,
Some(l) => l,
None => continue,
};
match lib.kind {
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&name.as_str()),
NativeLibraryKind::NativeFramework => cmd.link_framework(&name.as_str()),
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(&name.as_str()),
NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(&name.as_str(),
&search_path)
NativeLibraryKind::NativeUnknown => cmd.link_dylib(name),
NativeLibraryKind::NativeFramework => cmd.link_framework(name),
NativeLibraryKind::NativeStaticNobundle => cmd.link_staticlib(name),
NativeLibraryKind::NativeStatic => cmd.link_whole_staticlib(name, &search_path)
}
}
}
@ -1594,7 +1594,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(cmd: &mut dyn Linker,
cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
}
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
cmd.link_rust_dylib(&unlib(&sess.target, filestem),
cmd.link_rust_dylib(Symbol::intern(&unlib(&sess.target, filestem)),
parent.unwrap_or(Path::new("")));
}
}
@ -1637,22 +1637,22 @@ pub fn add_upstream_native_libraries(cmd: &mut dyn Linker,
for &(cnum, _) in crates {
for lib in codegen_results.crate_info.native_libraries[&cnum].iter() {
let name = match lib.name {
Some(ref l) => l,
Some(l) => l,
None => continue,
};
if !relevant_lib(sess, &lib) {
continue
}
match lib.kind {
NativeLibraryKind::NativeUnknown => cmd.link_dylib(&name.as_str()),
NativeLibraryKind::NativeFramework => cmd.link_framework(&name.as_str()),
NativeLibraryKind::NativeUnknown => cmd.link_dylib(name),
NativeLibraryKind::NativeFramework => cmd.link_framework(name),
NativeLibraryKind::NativeStaticNobundle => {
// Link "static-nobundle" native libs only if the crate they originate from
// is being linked statically to the current crate. If it's linked dynamically
// or is an rlib already included via some other dylib crate, the symbols from
// native libs will have already been included in that dylib.
if data[cnum.as_usize() - 1] == Linkage::Static {
cmd.link_staticlib(&name.as_str())
cmd.link_staticlib(name)
}
},
// ignore statically included native libraries here as we've

View file

@ -17,6 +17,7 @@ use rustc::session::config::{self, CrateType, OptLevel, DebugInfo,
use rustc::ty::TyCtxt;
use rustc_target::spec::{LinkerFlavor, LldFlavor};
use rustc_serialize::{json, Encoder};
use syntax::symbol::Symbol;
/// For all the linkers we support, and information they might
/// need out of the shared crate context before we get rid of it.
@ -99,13 +100,13 @@ impl LinkerInfo {
/// used to dispatch on whether a GNU-like linker (generally `ld.exe`) or an
/// MSVC linker (e.g., `link.exe`) is being used.
pub trait Linker {
fn link_dylib(&mut self, lib: &str);
fn link_rust_dylib(&mut self, lib: &str, path: &Path);
fn link_framework(&mut self, framework: &str);
fn link_staticlib(&mut self, lib: &str);
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: &str, search_path: &[PathBuf]);
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);
@ -215,9 +216,13 @@ impl<'a> GccLinker<'a> {
}
impl<'a> Linker for GccLinker<'a> {
fn link_dylib(&mut self, lib: &str) { self.hint_dynamic(); self.cmd.arg(format!("-l{}", lib)); }
fn link_staticlib(&mut self, lib: &str) {
self.hint_static(); self.cmd.arg(format!("-l{}", lib));
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); }
@ -232,14 +237,14 @@ impl<'a> Linker for GccLinker<'a> {
fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
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: &str) {
fn link_framework(&mut self, framework: Symbol) {
self.hint_dynamic();
self.cmd.arg("-framework").arg(framework);
self.cmd.arg("-framework").sym_arg(framework);
}
// Here we explicitly ask that the entire archive is included into the
@ -248,7 +253,7 @@ impl<'a> Linker for GccLinker<'a> {
// don't otherwise explicitly reference them. This can occur for
// libraries which are just providing bindings, libraries with generic
// functions, etc.
fn link_whole_staticlib(&mut self, lib: &str, search_path: &[PathBuf]) {
fn link_whole_staticlib(&mut self, lib: Symbol, search_path: &[PathBuf]) {
self.hint_static();
let target = &self.sess.target.target;
if !target.options.is_like_osx {
@ -539,11 +544,11 @@ impl<'a> Linker for MsvcLinker<'a> {
}
}
fn link_dylib(&mut self, lib: &str) {
fn link_dylib(&mut self, lib: Symbol) {
self.cmd.arg(&format!("{}.lib", lib));
}
fn link_rust_dylib(&mut self, lib: &str, path: &Path) {
fn link_rust_dylib(&mut self, lib: Symbol, path: &Path) {
// When producing a dll, the MSVC linker may not actually emit a
// `foo.lib` file if the dll doesn't actually export any symbols, so we
// check to see if the file is there and just omit linking to it if it's
@ -554,7 +559,7 @@ impl<'a> Linker for MsvcLinker<'a> {
}
}
fn link_staticlib(&mut self, lib: &str) {
fn link_staticlib(&mut self, lib: Symbol) {
self.cmd.arg(&format!("{}.lib", lib));
}
@ -605,11 +610,11 @@ impl<'a> Linker for MsvcLinker<'a> {
fn framework_path(&mut self, _path: &Path) {
bug!("frameworks are not supported on windows")
}
fn link_framework(&mut self, _framework: &str) {
fn link_framework(&mut self, _framework: Symbol) {
bug!("frameworks are not supported on windows")
}
fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
// not supported?
self.link_staticlib(lib);
}
@ -740,8 +745,8 @@ impl<'a> Linker for EmLinker<'a> {
self.cmd.arg("-L").arg(path);
}
fn link_staticlib(&mut self, lib: &str) {
self.cmd.arg("-l").arg(lib);
fn link_staticlib(&mut self, lib: Symbol) {
self.cmd.arg("-l").sym_arg(lib);
}
fn output_filename(&mut self, path: &Path) {
@ -752,12 +757,12 @@ impl<'a> Linker for EmLinker<'a> {
self.cmd.arg(path);
}
fn link_dylib(&mut self, lib: &str) {
fn link_dylib(&mut self, lib: Symbol) {
// Emscripten always links statically
self.link_staticlib(lib);
}
fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
fn link_whole_staticlib(&mut self, lib: Symbol, _search_path: &[PathBuf]) {
// not supported?
self.link_staticlib(lib);
}
@ -767,7 +772,7 @@ impl<'a> Linker for EmLinker<'a> {
self.link_rlib(lib);
}
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
self.link_dylib(lib);
}
@ -803,7 +808,7 @@ impl<'a> Linker for EmLinker<'a> {
bug!("frameworks are not supported on Emscripten")
}
fn link_framework(&mut self, _framework: &str) {
fn link_framework(&mut self, _framework: Symbol) {
bug!("frameworks are not supported on Emscripten")
}
@ -948,12 +953,12 @@ impl<'a> WasmLd<'a> {
}
impl<'a> Linker for WasmLd<'a> {
fn link_dylib(&mut self, lib: &str) {
self.cmd.arg("-l").arg(lib);
fn link_dylib(&mut self, lib: Symbol) {
self.cmd.arg("-l").sym_arg(lib);
}
fn link_staticlib(&mut self, lib: &str) {
self.cmd.arg("-l").arg(lib);
fn link_staticlib(&mut self, lib: Symbol) {
self.cmd.arg("-l").sym_arg(lib);
}
fn link_rlib(&mut self, lib: &Path) {
@ -995,16 +1000,16 @@ impl<'a> Linker for WasmLd<'a> {
self.cmd.args(args);
}
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
self.cmd.arg("-l").arg(lib);
fn link_rust_dylib(&mut self, lib: Symbol, _path: &Path) {
self.cmd.arg("-l").sym_arg(lib);
}
fn link_framework(&mut self, _framework: &str) {
fn link_framework(&mut self, _framework: Symbol) {
panic!("frameworks not supported")
}
fn link_whole_staticlib(&mut self, lib: &str, _search_path: &[PathBuf]) {
self.cmd.arg("-l").arg(lib);
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) {
@ -1162,19 +1167,19 @@ impl<'a> Linker for PtxLinker<'a> {
::std::mem::replace(&mut self.cmd, Command::new(""))
}
fn link_dylib(&mut self, _lib: &str) {
fn link_dylib(&mut self, _lib: Symbol) {
panic!("external dylibs not supported")
}
fn link_rust_dylib(&mut self, _lib: &str, _path: &Path) {
fn link_rust_dylib(&mut self, _lib: Symbol, _path: &Path) {
panic!("external dylibs not supported")
}
fn link_staticlib(&mut self, _lib: &str) {
fn link_staticlib(&mut self, _lib: Symbol) {
panic!("staticlibs not supported")
}
fn link_whole_staticlib(&mut self, _lib: &str, _search_path: &[PathBuf]) {
fn link_whole_staticlib(&mut self, _lib: Symbol, _search_path: &[PathBuf]) {
panic!("staticlibs not supported")
}
@ -1182,7 +1187,7 @@ impl<'a> Linker for PtxLinker<'a> {
panic!("frameworks not supported")
}
fn link_framework(&mut self, _framework: &str) {
fn link_framework(&mut self, _framework: Symbol) {
panic!("frameworks not supported")
}