diff options
author | Laurențiu Nicola <[email protected]> | 2020-12-11 12:53:22 +0000 |
---|---|---|
committer | Laurențiu Nicola <[email protected]> | 2020-12-13 12:08:59 +0000 |
commit | dc519b88aff2536a9c6cfdd12621ca73df26a5ef (patch) | |
tree | ca3a5741c6b746d3fb8dec911ec42736e4598e26 /crates/proc_macro_srv/src | |
parent | 41321d96789ed918eebda02ada76758765d19d16 (diff) |
Replace goblin crate with object
Diffstat (limited to 'crates/proc_macro_srv/src')
-rw-r--r-- | crates/proc_macro_srv/src/dylib.rs | 65 |
1 files changed, 27 insertions, 38 deletions
diff --git a/crates/proc_macro_srv/src/dylib.rs b/crates/proc_macro_srv/src/dylib.rs index f8f705da8..2afb973cc 100644 --- a/crates/proc_macro_srv/src/dylib.rs +++ b/crates/proc_macro_srv/src/dylib.rs | |||
@@ -1,14 +1,17 @@ | |||
1 | //! Handles dynamic library loading for proc macro | 1 | //! Handles dynamic library loading for proc macro |
2 | 2 | ||
3 | use crate::{proc_macro::bridge, rustc_server::TokenStream}; | 3 | use std::{ |
4 | use std::fs::File; | 4 | fs::File, |
5 | use std::path::{Path, PathBuf}; | 5 | io, |
6 | path::{Path, PathBuf}, | ||
7 | }; | ||
6 | 8 | ||
7 | use goblin::{mach::Mach, Object}; | ||
8 | use libloading::Library; | 9 | use libloading::Library; |
9 | use memmap::Mmap; | 10 | use memmap::Mmap; |
11 | use object::Object; | ||
10 | use proc_macro_api::ProcMacroKind; | 12 | use proc_macro_api::ProcMacroKind; |
11 | use std::io; | 13 | |
14 | use crate::{proc_macro::bridge, rustc_server::TokenStream}; | ||
12 | 15 | ||
13 | const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; | 16 | const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; |
14 | 17 | ||
@@ -23,40 +26,26 @@ fn is_derive_registrar_symbol(symbol: &str) -> bool { | |||
23 | fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> { | 26 | fn find_registrar_symbol(file: &Path) -> io::Result<Option<String>> { |
24 | let file = File::open(file)?; | 27 | let file = File::open(file)?; |
25 | let buffer = unsafe { Mmap::map(&file)? }; | 28 | let buffer = unsafe { Mmap::map(&file)? }; |
26 | let object = Object::parse(&buffer).map_err(invalid_data_err)?; | ||
27 | 29 | ||
28 | let name = match object { | 30 | Ok(object::File::parse(&buffer) |
29 | Object::Elf(elf) => { | 31 | .map_err(invalid_data_err)? |
30 | let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?; | 32 | .exports() |
31 | symbols.into_iter().find(|s| is_derive_registrar_symbol(s)).map(&str::to_owned) | 33 | .map_err(invalid_data_err)? |
32 | } | 34 | .into_iter() |
33 | Object::PE(pe) => pe | 35 | .map(|export| export.name()) |
34 | .exports | 36 | .filter_map(|sym| String::from_utf8(sym.into()).ok()) |
35 | .iter() | 37 | .find(|sym| is_derive_registrar_symbol(sym)) |
36 | .flat_map(|s| s.name) | 38 | .map(|sym| { |
37 | .find(|s| is_derive_registrar_symbol(s)) | 39 | // From MacOS docs: |
38 | .map(&str::to_owned), | 40 | // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html |
39 | Object::Mach(Mach::Binary(binary)) => { | 41 | // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be |
40 | let exports = binary.exports().map_err(invalid_data_err)?; | 42 | // prepended with an underscore. |
41 | exports | 43 | if cfg!(target_os = "macos") && sym.starts_with('_') { |
42 | .iter() | 44 | sym[1..].to_owned() |
43 | .map(|s| { | 45 | } else { |
44 | // In macos doc: | 46 | sym |
45 | // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlsym.3.html | 47 | } |
46 | // Unlike other dyld API's, the symbol name passed to dlsym() must NOT be | 48 | })) |
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); | ||
60 | } | 49 | } |
61 | 50 | ||
62 | /// Loads dynamic library in platform dependent manner. | 51 | /// Loads dynamic library in platform dependent manner. |