Skip to content

Replace goblin crate with object #6817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 8 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/proc_macro_srv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
65 changes: 27 additions & 38 deletions crates/proc_macro_srv/src/dylib.rs
Original file line number Diff line number Diff line change
@@ -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_";

Expand All @@ -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.
Expand Down