1 // Copyright 2012 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 //! Finds crate binaries and loads their metadata
13 use back::archive::{ArchiveRO, METADATA_FILENAME};
15 use driver::session::Session;
16 use lib::llvm::{False, llvm, ObjectFile, mk_section_iter};
17 use metadata::cstore::{MetadataBlob, MetadataVec, MetadataArchive};
18 use metadata::decoder;
19 use metadata::encoder;
20 use metadata::filesearch::{FileSearch, FileMatches, FileDoesntMatch};
22 use syntax::codemap::Span;
23 use syntax::diagnostic::SpanHandler;
24 use syntax::crateid::CrateId;
25 use syntax::attr::AttrMetaMethods;
28 use std::c_str::ToCStr;
36 use std::collections::{HashMap, HashSet};
40 pub static MACOS_DLL_PREFIX: &'static str = "lib";
41 pub static MACOS_DLL_SUFFIX: &'static str = ".dylib";
43 pub static WIN32_DLL_PREFIX: &'static str = "";
44 pub static WIN32_DLL_SUFFIX: &'static str = ".dll";
46 pub static LINUX_DLL_PREFIX: &'static str = "lib";
47 pub static LINUX_DLL_SUFFIX: &'static str = ".so";
49 pub static FREEBSD_DLL_PREFIX: &'static str = "lib";
50 pub static FREEBSD_DLL_SUFFIX: &'static str = ".so";
52 pub static ANDROID_DLL_PREFIX: &'static str = "lib";
53 pub static ANDROID_DLL_SUFFIX: &'static str = ".so";
55 pub struct CrateMismatch {
60 pub struct Context<'a> {
61 pub sess: &'a Session,
64 pub crate_id: &'a CrateId,
66 pub hash: Option<&'a Svh>,
69 pub filesearch: FileSearch<'a>,
70 pub root: &'a Option<CratePaths>,
71 pub rejected_via_hash: Vec<CrateMismatch>,
72 pub rejected_via_triple: Vec<CrateMismatch>,
76 pub dylib: Option<Path>,
77 pub rlib: Option<Path>,
78 pub metadata: MetadataBlob,
81 pub struct ArchiveMetadata {
83 // See comments in ArchiveMetadata::new for why this is static
87 pub struct CratePaths {
89 pub dylib: Option<Path>,
90 pub rlib: Option<Path>
94 fn paths(&self) -> Vec<Path> {
95 match (&self.dylib, &self.rlib) {
96 (&None, &None) => vec!(),
97 (&Some(ref p), &None) |
98 (&None, &Some(ref p)) => vec!(p.clone()),
99 (&Some(ref p1), &Some(ref p2)) => vec!(p1.clone(), p2.clone()),
104 impl<'a> Context<'a> {
105 pub fn maybe_load_library_crate(&mut self) -> Option<Library> {
106 self.find_library_crate()
109 pub fn load_library_crate(&mut self) -> Library {
110 match self.find_library_crate() {
113 self.report_load_errs();
119 pub fn report_load_errs(&mut self) {
120 let message = if self.rejected_via_hash.len() > 0 {
121 format!("found possibly newer version of crate `{}`",
123 } else if self.rejected_via_triple.len() > 0 {
124 format!("found incorrect triple for crate `{}`", self.ident)
126 format!("can't find crate for `{}`", self.ident)
128 let message = match self.root {
130 &Some(ref r) => format!("{} which `{}` depends on",
133 self.sess.span_err(self.span, message.as_slice());
135 let mismatches = self.rejected_via_triple.iter();
136 if self.rejected_via_triple.len() > 0 {
137 self.sess.span_note(self.span,
138 format!("expected triple of {}",
139 self.triple).as_slice());
140 for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() {
141 self.sess.fileline_note(self.span,
142 format!("crate `{}` path {}{}, triple {}: {}",
143 self.ident, "#", i+1, got, path.display()).as_slice());
146 if self.rejected_via_hash.len() > 0 {
147 self.sess.span_note(self.span, "perhaps this crate needs \
149 let mismatches = self.rejected_via_hash.iter();
150 for (i, &CrateMismatch{ ref path, .. }) in mismatches.enumerate() {
151 self.sess.fileline_note(self.span,
152 format!("crate `{}` path {}{}: {}",
153 self.ident, "#", i+1, path.display()).as_slice());
158 for (i, path) in r.paths().iter().enumerate() {
159 self.sess.fileline_note(self.span,
160 format!("crate `{}` path #{}: {}",
161 r.ident, i+1, path.display()).as_slice());
166 self.sess.abort_if_errors();
169 fn find_library_crate(&mut self) -> Option<Library> {
170 let dypair = self.dylibname();
172 // want: crate_name.dir_part() + prefix + crate_name.file_part + "-"
173 let dylib_prefix = dypair.map(|(prefix, _)| {
174 format!("{}{}-", prefix, self.crate_id.name)
176 let rlib_prefix = format!("lib{}-", self.crate_id.name);
178 let mut candidates = HashMap::new();
180 // First, find all possible candidate rlibs and dylibs purely based on
181 // the name of the files themselves. We're trying to match against an
182 // exact crate_id and a possibly an exact hash.
184 // During this step, we can filter all found libraries based on the
185 // name and id found in the crate id (we ignore the path portion for
186 // filename matching), as well as the exact hash (if specified). If we
187 // end up having many candidates, we must look at the metadata to
188 // perform exact matches against hashes/crate ids. Note that opening up
189 // the metadata is where we do an exact match against the full contents
190 // of the crate id (path/name/id).
192 // The goal of this step is to look at as little metadata as possible.
193 self.filesearch.search(|path| {
194 let file = match path.filename_str() {
195 None => return FileDoesntMatch,
198 if file.starts_with(rlib_prefix.as_slice()) &&
199 file.ends_with(".rlib") {
200 info!("rlib candidate: {}", path.display());
201 match self.try_match(file, rlib_prefix.as_slice(), ".rlib") {
203 info!("rlib accepted, hash: {}", hash);
204 let slot = candidates.find_or_insert_with(hash, |_| {
205 (HashSet::new(), HashSet::new())
207 let (ref mut rlibs, _) = *slot;
208 rlibs.insert(fs::realpath(path).unwrap());
212 info!("rlib rejected");
216 } else if dypair.map_or(false, |(_, suffix)| {
217 file.starts_with(dylib_prefix.get_ref().as_slice()) &&
218 file.ends_with(suffix)
220 let (_, suffix) = dypair.unwrap();
221 let dylib_prefix = dylib_prefix.get_ref().as_slice();
222 info!("dylib candidate: {}", path.display());
223 match self.try_match(file, dylib_prefix, suffix) {
225 info!("dylib accepted, hash: {}", hash);
226 let slot = candidates.find_or_insert_with(hash, |_| {
227 (HashSet::new(), HashSet::new())
229 let (_, ref mut dylibs) = *slot;
230 dylibs.insert(fs::realpath(path).unwrap());
234 info!("dylib rejected");
243 // We have now collected all known libraries into a set of candidates
244 // keyed of the filename hash listed. For each filename, we also have a
245 // list of rlibs/dylibs that apply. Here, we map each of these lists
246 // (per hash), to a Library candidate for returning.
248 // A Library candidate is created if the metadata for the set of
249 // libraries corresponds to the crate id and hash criteria that this
250 // search is being performed for.
251 let mut libraries = Vec::new();
252 for (_hash, (rlibs, dylibs)) in candidates.move_iter() {
253 let mut metadata = None;
254 let rlib = self.extract_one(rlibs, "rlib", &mut metadata);
255 let dylib = self.extract_one(dylibs, "dylib", &mut metadata);
258 libraries.push(Library {
268 // Having now translated all relevant found hashes into libraries, see
269 // what we've got and figure out if we found multiple candidates for
271 match libraries.len() {
273 1 => Some(libraries.move_iter().next().unwrap()),
275 self.sess.span_err(self.span,
276 format!("multiple matching crates for `{}`",
277 self.crate_id.name).as_slice());
278 self.sess.note("candidates:");
279 for lib in libraries.iter() {
282 self.sess.note(format!("path: {}",
283 p.display()).as_slice());
289 self.sess.note(format!("path: {}",
290 p.display()).as_slice());
294 let data = lib.metadata.as_slice();
295 let crate_id = decoder::get_crate_id(data);
296 note_crateid_attr(self.sess.diagnostic(), &crate_id);
303 // Attempts to match the requested version of a library against the file
304 // specified. The prefix/suffix are specified (disambiguates between
307 // The return value is `None` if `file` doesn't look like a rust-generated
308 // library, or if a specific version was requested and it doesn't match the
309 // apparent file's version.
311 // If everything checks out, then `Some(hash)` is returned where `hash` is
312 // the listed hash in the filename itself.
313 fn try_match(&self, file: &str, prefix: &str, suffix: &str) -> Option<String>{
314 let middle = file.slice(prefix.len(), file.len() - suffix.len());
315 debug!("matching -- {}, middle: {}", file, middle);
316 let mut parts = middle.splitn('-', 1);
317 let hash = match parts.next() { Some(h) => h, None => return None };
318 debug!("matching -- {}, hash: {} (want {})", file, hash, self.id_hash);
319 let vers = match parts.next() { Some(v) => v, None => return None };
320 debug!("matching -- {}, vers: {} (want {})", file, vers,
321 self.crate_id.version);
322 match self.crate_id.version {
323 Some(ref version) if version.as_slice() != vers => return None,
324 Some(..) => {} // check the hash
326 // hash is irrelevant, no version specified
327 None => return Some(hash.to_string())
329 debug!("matching -- {}, vers ok", file);
330 // hashes in filenames are prefixes of the "true hash"
331 if self.id_hash == hash.as_slice() {
332 debug!("matching -- {}, hash ok", file);
333 Some(hash.to_string())
339 // Attempts to extract *one* library from the set `m`. If the set has no
340 // elements, `None` is returned. If the set has more than one element, then
341 // the errors and notes are emitted about the set of libraries.
343 // With only one library in the set, this function will extract it, and then
344 // read the metadata from it if `*slot` is `None`. If the metadata couldn't
345 // be read, it is assumed that the file isn't a valid rust library (no
346 // errors are emitted).
347 fn extract_one(&mut self, m: HashSet<Path>, flavor: &str,
348 slot: &mut Option<MetadataBlob>) -> Option<Path> {
349 let mut ret = None::<Path>;
353 // FIXME(#10786): for an optimization, we only read one of the
354 // library's metadata sections. In theory we should
355 // read both, but reading dylib metadata is quite
359 } else if m.len() == 1 {
360 return Some(m.move_iter().next().unwrap())
364 for lib in m.move_iter() {
365 info!("{} reading metadata from: {}", flavor, lib.display());
366 let metadata = match get_metadata_section(self.os, &lib) {
368 if self.crate_matches(blob.as_slice(), &lib) {
371 info!("metadata mismatch");
376 info!("no metadata found");
381 self.sess.span_err(self.span,
382 format!("multiple {} candidates for `{}` \
385 self.crate_id.name).as_slice());
386 self.sess.span_note(self.span,
387 format!(r"candidate #1: {}",
389 .display()).as_slice());
395 self.sess.span_note(self.span,
396 format!(r"candidate #{}: {}", error,
397 lib.display()).as_slice());
400 *slot = Some(metadata);
403 return if error > 0 {None} else {ret}
406 fn crate_matches(&mut self, crate_data: &[u8], libpath: &Path) -> bool {
407 match decoder::maybe_get_crate_id(crate_data) {
408 Some(ref id) if self.crate_id.matches(id) => {}
409 _ => { info!("Rejecting via crate_id"); return false }
411 let hash = match decoder::maybe_get_crate_hash(crate_data) {
412 Some(hash) => hash, None => {
413 info!("Rejecting via lack of crate hash");
418 let triple = decoder::get_crate_triple(crate_data);
419 if triple.as_slice() != self.triple {
420 info!("Rejecting via crate triple: expected {} got {}", self.triple, triple);
421 self.rejected_via_triple.push(CrateMismatch {
422 path: libpath.clone(),
423 got: triple.to_string()
432 info!("Rejecting via hash: expected {} got {}", *myhash, hash);
433 self.rejected_via_hash.push(CrateMismatch {
434 path: libpath.clone(),
435 got: myhash.as_str().to_string()
446 // Returns the corresponding (prefix, suffix) that files need to have for
448 fn dylibname(&self) -> Option<(&'static str, &'static str)> {
450 abi::OsWin32 => Some((WIN32_DLL_PREFIX, WIN32_DLL_SUFFIX)),
451 abi::OsMacos => Some((MACOS_DLL_PREFIX, MACOS_DLL_SUFFIX)),
452 abi::OsLinux => Some((LINUX_DLL_PREFIX, LINUX_DLL_SUFFIX)),
453 abi::OsAndroid => Some((ANDROID_DLL_PREFIX, ANDROID_DLL_SUFFIX)),
454 abi::OsFreebsd => Some((FREEBSD_DLL_PREFIX, FREEBSD_DLL_SUFFIX)),
461 pub fn note_crateid_attr(diag: &SpanHandler, crateid: &CrateId) {
462 diag.handler().note(format!("crate_id: {}", crateid.to_str()).as_slice());
465 impl ArchiveMetadata {
466 fn new(ar: ArchiveRO) -> Option<ArchiveMetadata> {
467 let data: &'static [u8] = {
468 let data = match ar.read(METADATA_FILENAME) {
471 debug!("didn't find '{}' in the archive", METADATA_FILENAME);
475 // This data is actually a pointer inside of the archive itself, but
476 // we essentially want to cache it because the lookup inside the
477 // archive is a fairly expensive operation (and it's queried for
478 // *very* frequently). For this reason, we transmute it to the
479 // static lifetime to put into the struct. Note that the buffer is
480 // never actually handed out with a static lifetime, but rather the
481 // buffer is loaned with the lifetime of this containing object.
482 // Hence, we're guaranteed that the buffer will never be used after
483 // this object is dead, so this is a safe operation to transmute and
484 // store the data as a static buffer.
485 unsafe { mem::transmute(data) }
487 Some(ArchiveMetadata {
493 pub fn as_slice<'a>(&'a self) -> &'a [u8] { self.data }
496 // Just a small wrapper to time how long reading metadata takes.
497 fn get_metadata_section(os: abi::Os, filename: &Path) -> Result<MetadataBlob, String> {
498 let start = time::precise_time_ns();
499 let ret = get_metadata_section_imp(os, filename);
500 info!("reading {} => {}ms", filename.filename_display(),
501 (time::precise_time_ns() - start) / 1000000);
505 fn get_metadata_section_imp(os: abi::Os, filename: &Path) -> Result<MetadataBlob, String> {
506 if !filename.exists() {
507 return Err(format!("no such file: '{}'", filename.display()));
509 if filename.filename_str().unwrap().ends_with(".rlib") {
510 // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
511 // internally to read the file. We also avoid even using a memcpy by
512 // just keeping the archive along while the metadata is in use.
513 let archive = match ArchiveRO::open(filename) {
516 debug!("llvm didn't like `{}`", filename.display());
517 return Err(format!("failed to read rlib metadata: '{}'",
518 filename.display()));
521 return match ArchiveMetadata::new(archive).map(|ar| MetadataArchive(ar)) {
523 return Err((format!("failed to read rlib metadata: '{}'",
524 filename.display())))
526 Some(blob) => return Ok(blob)
530 let mb = filename.with_c_str(|buf| {
531 llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
534 return Err(format!("error reading library: '{}'",
537 let of = match ObjectFile::new(mb) {
540 return Err((format!("provided path not an object file: '{}'",
541 filename.display())))
544 let si = mk_section_iter(of.llof);
545 while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
546 let mut name_buf = ptr::null();
547 let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
548 let name = str::raw::from_buf_len(name_buf as *const u8,
550 debug!("get_metadata_section: name {}", name);
551 if read_meta_section_name(os).as_slice() == name.as_slice() {
552 let cbuf = llvm::LLVMGetSectionContents(si.llsi);
553 let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
555 Err(format!("metadata not found: '{}'", filename.display()));
556 let cvbuf: *const u8 = mem::transmute(cbuf);
557 let vlen = encoder::metadata_encoding_version.len();
558 debug!("checking {} bytes of metadata-version stamp",
560 let minsz = cmp::min(vlen, csz);
561 let version_ok = slice::raw::buf_as_slice(cvbuf, minsz,
562 |buf0| buf0 == encoder::metadata_encoding_version);
564 return Err((format!("incompatible metadata version found: '{}'",
565 filename.display())));
568 let cvbuf1 = cvbuf.offset(vlen as int);
569 debug!("inflating {} bytes of compressed metadata",
571 slice::raw::buf_as_slice(cvbuf1, csz-vlen, |bytes| {
572 match flate::inflate_bytes(bytes) {
573 Some(inflated) => found = Ok(MetadataVec(inflated)),
576 Err(format!("failed to decompress \
586 llvm::LLVMMoveToNextSection(si.llsi);
588 return Err(format!("metadata not found: '{}'", filename.display()));
592 pub fn meta_section_name(os: abi::Os) -> Option<&'static str> {
594 abi::OsMacos => Some("__DATA,__note.rustc"),
595 abi::OsiOS => Some("__DATA,__note.rustc"),
596 abi::OsWin32 => Some(".note.rustc"),
597 abi::OsLinux => Some(".note.rustc"),
598 abi::OsAndroid => Some(".note.rustc"),
599 abi::OsFreebsd => Some(".note.rustc")
603 pub fn read_meta_section_name(os: abi::Os) -> &'static str {
605 abi::OsMacos => "__note.rustc",
606 abi::OsiOS => unreachable!(),
607 abi::OsWin32 => ".note.rustc",
608 abi::OsLinux => ".note.rustc",
609 abi::OsAndroid => ".note.rustc",
610 abi::OsFreebsd => ".note.rustc"
614 // A diagnostic function for dumping crate metadata to an output stream
615 pub fn list_file_metadata(os: abi::Os, path: &Path,
616 out: &mut io::Writer) -> io::IoResult<()> {
617 match get_metadata_section(os, path) {
618 Ok(bytes) => decoder::list_crate_metadata(bytes.as_slice(), out),
620 write!(out, "{}\n", msg)