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",
"libc",
"miniz_oxide",
"object",
"object 0.22.0",
"rustc-demangle",
]
@ -499,17 +499,6 @@ version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "hashbrown"
version = "0.9.1"
@ -1054,6 +1043,12 @@ version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
[[package]]
name = "object"
version = "0.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]]
name = "once_cell"
version = "1.5.2"
@ -1158,12 +1153,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b063f57ec186e6140e2b8b6921e5f1bd89c7356dda5b33acc5401203ca6131c"
[[package]]
name = "plain"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
[[package]]
name = "proc-macro2"
version = "1.0.24"
@ -1192,10 +1181,10 @@ version = "0.0.0"
dependencies = [
"cargo_metadata",
"difference",
"goblin",
"libloading",
"mbe",
"memmap",
"object 0.23.0",
"proc_macro_api",
"proc_macro_test",
"serde_derive",
@ -1471,26 +1460,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "semver"
version = "0.9.0"

View file

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

View file

@ -1,14 +1,17 @@
//! Handles dynamic library loading for proc macro
use crate::{proc_macro::bridge, rustc_server::TokenStream};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::{
fs::File,
io,
path::{Path, PathBuf},
};
use goblin::{mach::Mach, Object};
use libloading::Library;
use memmap::Mmap;
use object::Object;
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_";
@ -23,40 +26,26 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool {
fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> {
let file = File::open(file)?;
let buffer = unsafe { Mmap::map(&file)? };
let object = Object::parse(&buffer).map_err(invalid_data_err)?;
let name = match object {
Object::Elf(elf) => {
let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?;
symbols.into_iter().find(|s| is_derive_registrar_symbol(s)).map(&str::to_owned)
}
Object::PE(pe) => pe
.exports
.iter()
.flat_map(|s| s.name)
.find(|s| is_derive_registrar_symbol(s))
.map(&str::to_owned),
Object::Mach(Mach::Binary(binary)) => {
let exports = binary.exports().map_err(invalid_data_err)?;
exports
.iter()
.map(|s| {
// In macos doc:
// 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);
Ok(object::File::parse(&buffer)
.map_err(invalid_data_err)?
.exports()
.map_err(invalid_data_err)?
.into_iter()
.map(|export| export.name())
.filter_map(|sym| String::from_utf8(sym.into()).ok())
.find(|sym| is_derive_registrar_symbol(sym))
.map(|sym| {
// From MacOS docs:
// 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 cfg!(target_os = "macos") && sym.starts_with('_') {
sym[1..].to_owned()
} else {
sym
}
}))
}
/// Loads dynamic library in platform dependent manner.