Replace goblin crate with object

This commit is contained in:
Laurențiu Nicola 2020-12-11 14:53:22 +02:00
parent 41321d9678
commit dc519b88af
3 changed files with 36 additions and 78 deletions

47
Cargo.lock generated
View file

@ -89,7 +89,7 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"libc", "libc",
"miniz_oxide", "miniz_oxide",
"object", "object 0.22.0",
"rustc-demangle", "rustc-demangle",
] ]
@ -499,17 +499,6 @@ version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce" checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
[[package]]
name = "goblin"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d20fd25aa456527ce4f544271ae4fea65d2eda4a6561ea56f39fb3ee4f7e3884"
dependencies = [
"log",
"plain",
"scroll",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.9.1" version = "0.9.1"
@ -1054,6 +1043,12 @@ version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
[[package]]
name = "object"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.5.2" version = "1.5.2"
@ -1158,12 +1153,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c" checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c"
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.24" version = "1.0.24"
@ -1192,10 +1181,10 @@ version = "0.0.0"
dependencies = [ dependencies = [
"cargo_metadata", "cargo_metadata",
"difference", "difference",
"goblin",
"libloading", "libloading",
"mbe", "mbe",
"memmap", "memmap",
"object 0.23.0",
"proc_macro_api", "proc_macro_api",
"proc_macro_test", "proc_macro_test",
"serde_derive", "serde_derive",
@ -1471,26 +1460,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "scroll"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fda28d4b4830b807a8b43f7b0e6b5df875311b3e7621d84577188c175b6ec1ec"
dependencies = [
"scroll_derive",
]
[[package]]
name = "scroll_derive"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b12bd20b94c7cdfda8c7ba9b92ad0d9a56e3fa018c25fca83b51aa664c9b4c0d"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "semver" name = "semver"
version = "0.9.0" version = "0.9.0"

View file

@ -10,7 +10,7 @@ edition = "2018"
doctest = false doctest = false
[dependencies] [dependencies]
goblin = "0.2.1" object = { version = "0.23", default-features = false, features = ["std", "read_core", "elf", "macho", "pe", "unaligned"] }
libloading = "0.6.0" libloading = "0.6.0"
memmap = "0.7" memmap = "0.7"

View file

@ -1,14 +1,17 @@
//! Handles dynamic library loading for proc macro //! Handles dynamic library loading for proc macro
use crate::{proc_macro::bridge, rustc_server::TokenStream}; use std::{
use std::fs::File; fs::File,
use std::path::{Path, PathBuf}; io,
path::{Path, PathBuf},
};
use goblin::{mach::Mach, Object};
use libloading::Library; use libloading::Library;
use memmap::Mmap; use memmap::Mmap;
use object::Object;
use proc_macro_api::ProcMacroKind; use proc_macro_api::ProcMacroKind;
use std::io;
use crate::{proc_macro::bridge, rustc_server::TokenStream};
const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_";
@ -23,40 +26,26 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool {
fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> { fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> {
let file = File::open(file)?; let file = File::open(file)?;
let buffer = unsafe { Mmap::map(&file)? }; let buffer = unsafe { Mmap::map(&file)? };
let object = Object::parse(&buffer).map_err(invalid_data_err)?;
let name = match object { Ok(object::File::parse(&buffer)
Object::Elf(elf) => { .map_err(invalid_data_err)?
let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?; .exports()
symbols.into_iter().find(|s| is_derive_registrar_symbol(s)).map(&str::to_owned) .map_err(invalid_data_err)?
} .into_iter()
Object::PE(pe) => pe .map(|export| export.name())
.exports .filter_map(|sym| String::from_utf8(sym.into()).ok())
.iter() .find(|sym| is_derive_registrar_symbol(sym))
.flat_map(|s| s.name) .map(|sym| {
.find(|s| is_derive_registrar_symbol(s)) // From MacOS docs:
.map(&str::to_owned), // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html
Object::Mach(Mach::Binary(binary)) => { // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be
let exports = binary.exports().map_err(invalid_data_err)?; // prepended with an underscore.
exports if cfg!(target_os = "macos") && sym.starts_with('_') {
.iter() sym[1..].to_owned()
.map(|s| { } else {
// In macos doc: sym
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html }
// Unlike other dyld API's, the symbol name passed to dlsym() must NOT be }))
// prepended with an underscore.
if s.name.starts_with('_') {
&s.name[1..]
} else {
&s.name
}
})
.find(|s| is_derive_registrar_symbol(s))
.map(&str::to_owned)
}
_ => return Ok(None),
};
return Ok(name);
} }
/// Loads dynamic library in platform dependent manner. /// Loads dynamic library in platform dependent manner.