Skip to content

Commit 134c756

Browse files
bors[bot]lnicola
andauthored
Merge #6817
6817: Replace goblin crate with object r=matklad a=lnicola Continuation of #4385. Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 8cba423 + dc519b8 commit 134c756

File tree

3 files changed

+36
-78
lines changed

3 files changed

+36
-78
lines changed

Cargo.lock

Lines changed: 8 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/proc_macro_srv/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2018"
1010
doctest = false
1111

1212
[dependencies]
13-
goblin = "0.2.1"
13+
object = { version = "0.23", default-features = false, features = ["std", "read_core", "elf", "macho", "pe", "unaligned"] }
1414
libloading = "0.6.0"
1515
memmap = "0.7"
1616

crates/proc_macro_srv/src/dylib.rs

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
//! Handles dynamic library loading for proc macro
22
3-
use crate::{proc_macro::bridge, rustc_server::TokenStream};
4-
use std::fs::File;
5-
use std::path::{Path, PathBuf};
3+
use std::{
4+
fs::File,
5+
io,
6+
path::{Path, PathBuf},
7+
};
68

7-
use goblin::{mach::Mach, Object};
89
use libloading::Library;
910
use memmap::Mmap;
11+
use object::Object;
1012
use proc_macro_api::ProcMacroKind;
11-
use std::io;
13+
14+
use crate::{proc_macro::bridge, rustc_server::TokenStream};
1215

1316
const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_";
1417

@@ -23,40 +26,26 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool {
2326
fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> {
2427
let file = File::open(file)?;
2528
let buffer = unsafe { Mmap::map(&file)? };
26-
let object = Object::parse(&buffer).map_err(invalid_data_err)?;
2729

28-
let name = match object {
29-
Object::Elf(elf) => {
30-
let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?;
31-
symbols.into_iter().find(|s| is_derive_registrar_symbol(s)).map(&str::to_owned)
32-
}
33-
Object::PE(pe) => pe
34-
.exports
35-
.iter()
36-
.flat_map(|s| s.name)
37-
.find(|s| is_derive_registrar_symbol(s))
38-
.map(&str::to_owned),
39-
Object::Mach(Mach::Binary(binary)) => {
40-
let exports = binary.exports().map_err(invalid_data_err)?;
41-
exports
42-
.iter()
43-
.map(|s| {
44-
// In macos doc:
45-
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html
46-
// Unlike other dyld API's, the symbol name passed to dlsym() must NOT be
47-
// prepended with an underscore.
48-
if s.name.starts_with('_') {
49-
&s.name[1..]
50-
} else {
51-
&s.name
52-
}
53-
})
54-
.find(|s| is_derive_registrar_symbol(s))
55-
.map(&str::to_owned)
56-
}
57-
_ => return Ok(None),
58-
};
59-
return Ok(name);
30+
Ok(object::File::parse(&buffer)
31+
.map_err(invalid_data_err)?
32+
.exports()
33+
.map_err(invalid_data_err)?
34+
.into_iter()
35+
.map(|export| export.name())
36+
.filter_map(|sym| String::from_utf8(sym.into()).ok())
37+
.find(|sym| is_derive_registrar_symbol(sym))
38+
.map(|sym| {
39+
// From MacOS docs:
40+
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html
41+
// Unlike other dyld API's, the symbol name passed to dlsym() must NOT be
42+
// prepended with an underscore.
43+
if cfg!(target_os = "macos") && sym.starts_with('_') {
44+
sym[1..].to_owned()
45+
} else {
46+
sym
47+
}
48+
}))
6049
}
6150

6251
/// Loads dynamic library in platform dependent manner.

0 commit comments

Comments
 (0)