1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::util::common;
12 use rustc::middle::cstore::MetadataLoader;
13 use rustc_back::target::Target;
15 use llvm::{False, ObjectFile, mk_section_iter};
16 use llvm::archive_ro::ArchiveRO;
18 use owning_ref::{ErasedBoxRef, OwningRef};
23 pub const METADATA_FILENAME: &str = "rust.metadata.bin";
25 pub struct LlvmMetadataLoader;
27 impl MetadataLoader for LlvmMetadataLoader {
28 fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<ErasedBoxRef<[u8]>, String> {
29 // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
30 // internally to read the file. We also avoid even using a memcpy by
31 // just keeping the archive along while the metadata is in use.
32 let archive = ArchiveRO::open(filename)
33 .map(|ar| OwningRef::new(box ar))
35 debug!("llvm didn't like `{}`: {}", filename.display(), e);
36 format!("failed to read rlib metadata in '{}': {}", filename.display(), e)
38 let buf: OwningRef<_, [u8]> = archive
41 .filter_map(|s| s.ok())
42 .find(|sect| sect.name() == Some(METADATA_FILENAME))
45 debug!("didn't find '{}' in the archive", METADATA_FILENAME);
46 format!("failed to read rlib metadata: '{}'",
53 fn get_dylib_metadata(&self,
56 -> Result<ErasedBoxRef<[u8]>, String> {
58 let buf = common::path2cstr(filename);
59 let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr());
61 return Err(format!("error reading library: '{}'", filename.display()));
63 let of = ObjectFile::new(mb)
64 .map(|of| OwningRef::new(box of))
65 .ok_or_else(|| format!("provided path not an object file: '{}'",
66 filename.display()))?;
67 let buf = of.try_map(|of| search_meta_section(of, target, filename))?;
73 fn search_meta_section<'a>(of: &'a ObjectFile,
76 -> Result<&'a [u8], String> {
78 let si = mk_section_iter(of.llof);
79 while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
80 let mut name_buf = ptr::null();
81 let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
82 let name = slice::from_raw_parts(name_buf as *const u8, name_len as usize).to_vec();
83 let name = String::from_utf8(name).unwrap();
84 debug!("get_metadata_section: name {}", name);
85 if read_metadata_section_name(target) == name {
86 let cbuf = llvm::LLVMGetSectionContents(si.llsi);
87 let csz = llvm::LLVMGetSectionSize(si.llsi) as usize;
88 // The buffer is valid while the object file is around
89 let buf: &'a [u8] = slice::from_raw_parts(cbuf as *const u8, csz);
92 llvm::LLVMMoveToNextSection(si.llsi);
95 Err(format!("metadata not found: '{}'", filename.display()))
98 pub fn metadata_section_name(target: &Target) -> &'static str {
101 // When using link.exe it was seen that the section name `.note.rustc`
102 // was getting shortened to `.note.ru`, and according to the PE and COFF
105 // > Executable images do not use a string table and do not support
106 // > section names longer than 8 characters
108 // https://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx
110 // As a result, we choose a slightly shorter name! As to why
111 // `.note.rustc` works on MinGW, that's another good question...
113 if target.options.is_like_osx {
120 fn read_metadata_section_name(_target: &Target) -> &'static str {