]> git.lizzy.rs Git - rust.git/blob - src/metadata.rs
Use either SimpleJIT or faerie, but not both
[rust.git] / src / metadata.rs
1 use rustc::middle::cstore::MetadataLoader;
2 use rustc_data_structures::owning_ref::{self, OwningRef};
3 use std::fs::File;
4 use std::path::Path;
5
6 pub struct CraneliftMetadataLoader;
7
8 impl MetadataLoader for CraneliftMetadataLoader {
9     fn get_rlib_metadata(
10         &self,
11         _target: &::rustc_target::spec::Target,
12         path: &Path,
13     ) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
14         let mut archive = ar::Archive::new(File::open(path).map_err(|e| format!("{:?}", e))?);
15         // Iterate over all entries in the archive:
16         while let Some(entry_result) = archive.next_entry() {
17             let mut entry = entry_result.map_err(|e| format!("{:?}", e))?;
18             if entry
19                 .header()
20                 .identifier()
21                 .starts_with(b".rustc.clif_metadata")
22             {
23                 let mut buf = Vec::new();
24                 ::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?;
25                 let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
26                 return Ok(rustc_erase_owner!(buf.map_owner_box()));
27             }
28         }
29
30         Err("couldn't find metadata entry".to_string())
31         //self.get_dylib_metadata(target, path)
32     }
33
34     fn get_dylib_metadata(
35         &self,
36         _target: &::rustc_target::spec::Target,
37         _path: &Path,
38     ) -> Result<owning_ref::ErasedBoxRef<[u8]>, String> {
39         //use goblin::Object;
40
41         //let buffer = ::std::fs::read(path).map_err(|e|format!("{:?}", e))?;
42         /*match Object::parse(&buffer).map_err(|e|format!("{:?}", e))? {
43             Object::Elf(elf) => {
44                 println!("elf: {:#?}", &elf);
45             },
46             Object::PE(pe) => {
47                 println!("pe: {:#?}", &pe);
48             },
49             Object::Mach(mach) => {
50                 println!("mach: {:#?}", &mach);
51             },
52             Object::Archive(archive) => {
53                 return Err(format!("archive: {:#?}", &archive));
54             },
55             Object::Unknown(magic) => {
56                 return Err(format!("unknown magic: {:#x}", magic))
57             }
58         }*/
59         Err("dylib metadata loading is not yet supported".to_string())
60     }
61 }