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};
21 use syntax::codemap::Span;
22 use syntax::diagnostic::SpanHandler;
23 use syntax::crateid::CrateId;
24 use syntax::attr::AttrMetaMethods;
27 use std::c_str::ToCStr;
35 use collections::{HashMap, HashSet};
39 pub static MACOS_DLL_PREFIX: &'static str = "lib";
40 pub static MACOS_DLL_SUFFIX: &'static str = ".dylib";
42 pub static WIN32_DLL_PREFIX: &'static str = "";
43 pub static WIN32_DLL_SUFFIX: &'static str = ".dll";
45 pub static LINUX_DLL_PREFIX: &'static str = "lib";
46 pub static LINUX_DLL_SUFFIX: &'static str = ".so";
48 pub static FREEBSD_DLL_PREFIX: &'static str = "lib";
49 pub static FREEBSD_DLL_SUFFIX: &'static str = ".so";
51 pub static ANDROID_DLL_PREFIX: &'static str = "lib";
52 pub static ANDROID_DLL_SUFFIX: &'static str = ".so";
62 pub struct CrateMismatch {
67 pub struct Context<'a> {
68 pub sess: &'a Session,
71 pub crate_id: &'a CrateId,
73 pub hash: Option<&'a Svh>,
76 pub filesearch: FileSearch<'a>,
77 pub root: &'a Option<CratePaths>,
78 pub rejected_via_hash: Vec<CrateMismatch>,
79 pub rejected_via_triple: Vec<CrateMismatch>,
83 pub dylib: Option<Path>,
84 pub rlib: Option<Path>,
85 pub metadata: MetadataBlob,
88 pub struct ArchiveMetadata {
90 // See comments in ArchiveMetadata::new for why this is static
94 pub struct CratePaths {
96 pub dylib: Option<Path>,
97 pub rlib: Option<Path>
101 fn paths(&self) -> Vec<Path> {
102 match (&self.dylib, &self.rlib) {
103 (&None, &None) => vec!(),
104 (&Some(ref p), &None) |
105 (&None, &Some(ref p)) => vec!(p.clone()),
106 (&Some(ref p1), &Some(ref p2)) => vec!(p1.clone(), p2.clone()),
111 impl<'a> Context<'a> {
112 pub fn maybe_load_library_crate(&mut self) -> Option<Library> {
113 self.find_library_crate()
116 pub fn load_library_crate(&mut self) -> Library {
117 match self.find_library_crate() {
120 self.report_load_errs();
126 pub fn report_load_errs(&mut self) {
127 let message = if self.rejected_via_hash.len() > 0 {
128 format!("found possibly newer version of crate `{}`",
130 } else if self.rejected_via_triple.len() > 0 {
131 format!("found incorrect triple for crate `{}`", self.ident)
133 format!("can't find crate for `{}`", self.ident)
135 let message = match self.root {
137 &Some(ref r) => format!("{} which `{}` depends on",
140 self.sess.span_err(self.span, message.as_slice());
142 let mismatches = self.rejected_via_triple.iter();
143 if self.rejected_via_triple.len() > 0 {
144 self.sess.span_note(self.span,
145 format!("expected triple of {}",
146 self.triple).as_slice());
147 for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() {
148 self.sess.fileline_note(self.span,
149 format!("crate `{}` path \\#{}, triple {}: {}",
150 self.ident, i+1, got, path.display()).as_slice());
153 if self.rejected_via_hash.len() > 0 {
154 self.sess.span_note(self.span, "perhaps this crate needs \
156 let mismatches = self.rejected_via_hash.iter();
157 for (i, &CrateMismatch{ ref path, .. }) in mismatches.enumerate() {
158 self.sess.fileline_note(self.span,
159 format!("crate `{}` path \\#{}: {}",
160 self.ident, i+1, path.display()).as_slice());
165 for (i, path) in r.paths().iter().enumerate() {
166 self.sess.fileline_note(self.span,
167 format!("crate `{}` path \\#{}: {}",
168 r.ident, i+1, path.display()).as_slice());
173 self.sess.abort_if_errors();
176 fn find_library_crate(&mut self) -> Option<Library> {
177 let (dyprefix, dysuffix) = self.dylibname();
179 // want: crate_name.dir_part() + prefix + crate_name.file_part + "-"
180 let dylib_prefix = format!("{}{}-", dyprefix, self.crate_id.name);
181 let rlib_prefix = format!("lib{}-", self.crate_id.name);
183 let mut candidates = HashMap::new();
185 // First, find all possible candidate rlibs and dylibs purely based on
186 // the name of the files themselves. We're trying to match against an
187 // exact crate_id and a possibly an exact hash.
189 // During this step, we can filter all found libraries based on the
190 // name and id found in the crate id (we ignore the path portion for
191 // filename matching), as well as the exact hash (if specified). If we
192 // end up having many candidates, we must look at the metadata to
193 // perform exact matches against hashes/crate ids. Note that opening up
194 // the metadata is where we do an exact match against the full contents
195 // of the crate id (path/name/id).
197 // The goal of this step is to look at as little metadata as possible.
198 self.filesearch.search(|path| {
199 let file = match path.filename_str() {
200 None => return FileDoesntMatch,
203 if file.starts_with(rlib_prefix.as_slice()) &&
204 file.ends_with(".rlib") {
205 info!("rlib candidate: {}", path.display());
206 match self.try_match(file, rlib_prefix.as_slice(), ".rlib") {
208 info!("rlib accepted, hash: {}", hash);
209 let slot = candidates.find_or_insert_with(hash, |_| {
210 (HashSet::new(), HashSet::new())
212 let (ref mut rlibs, _) = *slot;
213 rlibs.insert(fs::realpath(path).unwrap());
217 info!("rlib rejected");
221 } else if file.starts_with(dylib_prefix.as_slice()) &&
222 file.ends_with(dysuffix){
223 info!("dylib candidate: {}", path.display());
224 match self.try_match(file,
225 dylib_prefix.as_slice(),
228 info!("dylib accepted, hash: {}", hash);
229 let slot = candidates.find_or_insert_with(hash, |_| {
230 (HashSet::new(), HashSet::new())
232 let (_, ref mut dylibs) = *slot;
233 dylibs.insert(fs::realpath(path).unwrap());
237 info!("dylib rejected");
246 // We have now collected all known libraries into a set of candidates
247 // keyed of the filename hash listed. For each filename, we also have a
248 // list of rlibs/dylibs that apply. Here, we map each of these lists
249 // (per hash), to a Library candidate for returning.
251 // A Library candidate is created if the metadata for the set of
252 // libraries corresponds to the crate id and hash criteria that this
253 // search is being performed for.
254 let mut libraries = Vec::new();
255 for (_hash, (rlibs, dylibs)) in candidates.move_iter() {
256 let mut metadata = None;
257 let rlib = self.extract_one(rlibs, "rlib", &mut metadata);
258 let dylib = self.extract_one(dylibs, "dylib", &mut metadata);
261 libraries.push(Library {
271 // Having now translated all relevant found hashes into libraries, see
272 // what we've got and figure out if we found multiple candidates for
274 match libraries.len() {
276 1 => Some(libraries.move_iter().next().unwrap()),
278 self.sess.span_err(self.span,
279 format!("multiple matching crates for `{}`",
280 self.crate_id.name).as_slice());
281 self.sess.note("candidates:");
282 for lib in libraries.iter() {
285 self.sess.note(format!("path: {}",
286 p.display()).as_slice());
292 self.sess.note(format!("path: {}",
293 p.display()).as_slice());
297 let data = lib.metadata.as_slice();
298 let crate_id = decoder::get_crate_id(data);
299 note_crateid_attr(self.sess.diagnostic(), &crate_id);
306 // Attempts to match the requested version of a library against the file
307 // specified. The prefix/suffix are specified (disambiguates between
310 // The return value is `None` if `file` doesn't look like a rust-generated
311 // library, or if a specific version was requested and it doesn't match the
312 // apparent file's version.
314 // If everything checks out, then `Some(hash)` is returned where `hash` is
315 // the listed hash in the filename itself.
316 fn try_match(&self, file: &str, prefix: &str, suffix: &str) -> Option<String>{
317 let middle = file.slice(prefix.len(), file.len() - suffix.len());
318 debug!("matching -- {}, middle: {}", file, middle);
319 let mut parts = middle.splitn('-', 1);
320 let hash = match parts.next() { Some(h) => h, None => return None };
321 debug!("matching -- {}, hash: {} (want {})", file, hash, self.id_hash);
322 let vers = match parts.next() { Some(v) => v, None => return None };
323 debug!("matching -- {}, vers: {} (want {})", file, vers,
324 self.crate_id.version);
325 match self.crate_id.version {
326 Some(ref version) if version.as_slice() != vers => return None,
327 Some(..) => {} // check the hash
329 // hash is irrelevant, no version specified
330 None => return Some(hash.to_string())
332 debug!("matching -- {}, vers ok", file);
333 // hashes in filenames are prefixes of the "true hash"
334 if self.id_hash == hash.as_slice() {
335 debug!("matching -- {}, hash ok", file);
336 Some(hash.to_string())
342 // Attempts to extract *one* library from the set `m`. If the set has no
343 // elements, `None` is returned. If the set has more than one element, then
344 // the errors and notes are emitted about the set of libraries.
346 // With only one library in the set, this function will extract it, and then
347 // read the metadata from it if `*slot` is `None`. If the metadata couldn't
348 // be read, it is assumed that the file isn't a valid rust library (no
349 // errors are emitted).
350 fn extract_one(&mut self, m: HashSet<Path>, flavor: &str,
351 slot: &mut Option<MetadataBlob>) -> Option<Path> {
352 let mut ret = None::<Path>;
356 // FIXME(#10786): for an optimization, we only read one of the
357 // library's metadata sections. In theory we should
358 // read both, but reading dylib metadata is quite
362 } else if m.len() == 1 {
363 return Some(m.move_iter().next().unwrap())
367 for lib in m.move_iter() {
368 info!("{} reading metadata from: {}", flavor, lib.display());
369 let metadata = match get_metadata_section(self.os, &lib) {
371 if self.crate_matches(blob.as_slice(), &lib) {
374 info!("metadata mismatch");
379 info!("no metadata found");
384 self.sess.span_err(self.span,
385 format!("multiple {} candidates for `{}` \
388 self.crate_id.name).as_slice());
389 self.sess.span_note(self.span,
390 format!(r"candidate \#1: {}",
392 .display()).as_slice());
398 self.sess.span_note(self.span,
399 format!(r"candidate \#{}: {}", error,
400 lib.display()).as_slice());
403 *slot = Some(metadata);
406 return if error > 0 {None} else {ret}
409 fn crate_matches(&mut self, crate_data: &[u8], libpath: &Path) -> bool {
410 match decoder::maybe_get_crate_id(crate_data) {
411 Some(ref id) if self.crate_id.matches(id) => {}
412 _ => { info!("Rejecting via crate_id"); return false }
414 let hash = match decoder::maybe_get_crate_hash(crate_data) {
415 Some(hash) => hash, None => {
416 info!("Rejecting via lack of crate hash");
421 let triple = decoder::get_crate_triple(crate_data);
422 if triple.as_slice() != self.triple {
423 info!("Rejecting via crate triple: expected {} got {}", self.triple, triple);
424 self.rejected_via_triple.push(CrateMismatch {
425 path: libpath.clone(),
426 got: triple.to_string()
435 info!("Rejecting via hash: expected {} got {}", *myhash, hash);
436 self.rejected_via_hash.push(CrateMismatch {
437 path: libpath.clone(),
438 got: myhash.as_str().to_string()
449 // Returns the corresponding (prefix, suffix) that files need to have for
451 fn dylibname(&self) -> (&'static str, &'static str) {
453 OsWin32 => (WIN32_DLL_PREFIX, WIN32_DLL_SUFFIX),
454 OsMacos => (MACOS_DLL_PREFIX, MACOS_DLL_SUFFIX),
455 OsLinux => (LINUX_DLL_PREFIX, LINUX_DLL_SUFFIX),
456 OsAndroid => (ANDROID_DLL_PREFIX, ANDROID_DLL_SUFFIX),
457 OsFreebsd => (FREEBSD_DLL_PREFIX, FREEBSD_DLL_SUFFIX),
463 pub fn note_crateid_attr(diag: &SpanHandler, crateid: &CrateId) {
464 diag.handler().note(format!("crate_id: {}", crateid.to_str()).as_slice());
467 impl ArchiveMetadata {
468 fn new(ar: ArchiveRO) -> Option<ArchiveMetadata> {
469 let data: &'static [u8] = {
470 let data = match ar.read(METADATA_FILENAME) {
473 debug!("didn't find '{}' in the archive", METADATA_FILENAME);
477 // This data is actually a pointer inside of the archive itself, but
478 // we essentially want to cache it because the lookup inside the
479 // archive is a fairly expensive operation (and it's queried for
480 // *very* frequently). For this reason, we transmute it to the
481 // static lifetime to put into the struct. Note that the buffer is
482 // never actually handed out with a static lifetime, but rather the
483 // buffer is loaned with the lifetime of this containing object.
484 // Hence, we're guaranteed that the buffer will never be used after
485 // this object is dead, so this is a safe operation to transmute and
486 // store the data as a static buffer.
487 unsafe { mem::transmute(data) }
489 Some(ArchiveMetadata {
495 pub fn as_slice<'a>(&'a self) -> &'a [u8] { self.data }
498 // Just a small wrapper to time how long reading metadata takes.
499 fn get_metadata_section(os: Os, filename: &Path) -> Result<MetadataBlob, String> {
500 let start = time::precise_time_ns();
501 let ret = get_metadata_section_imp(os, filename);
502 info!("reading {} => {}ms", filename.filename_display(),
503 (time::precise_time_ns() - start) / 1000000);
507 fn get_metadata_section_imp(os: Os, filename: &Path) -> Result<MetadataBlob, String> {
508 if !filename.exists() {
509 return Err(format_strbuf!("no such file: '{}'", filename.display()));
511 if filename.filename_str().unwrap().ends_with(".rlib") {
512 // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
513 // internally to read the file. We also avoid even using a memcpy by
514 // just keeping the archive along while the metadata is in use.
515 let archive = match ArchiveRO::open(filename) {
518 debug!("llvm didn't like `{}`", filename.display());
519 return Err(format_strbuf!("failed to read rlib metadata: \
521 filename.display()));
524 return match ArchiveMetadata::new(archive).map(|ar| MetadataArchive(ar)) {
526 return Err((format_strbuf!("failed to read rlib metadata: \
528 filename.display())))
530 Some(blob) => return Ok(blob)
534 let mb = filename.with_c_str(|buf| {
535 llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
538 return Err(format_strbuf!("error reading library: '{}'",
541 let of = match ObjectFile::new(mb) {
544 return Err((format_strbuf!("provided path not an object \
546 filename.display())))
549 let si = mk_section_iter(of.llof);
550 while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
551 let mut name_buf = ptr::null();
552 let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
553 let name = str::raw::from_buf_len(name_buf as *u8, name_len as uint);
554 debug!("get_metadata_section: name {}", name);
555 if read_meta_section_name(os).as_slice() == name.as_slice() {
556 let cbuf = llvm::LLVMGetSectionContents(si.llsi);
557 let csz = llvm::LLVMGetSectionSize(si.llsi) as uint;
559 Err(format_strbuf!("metadata not found: '{}'",
560 filename.display()));
561 let cvbuf: *u8 = mem::transmute(cbuf);
562 let vlen = encoder::metadata_encoding_version.len();
563 debug!("checking {} bytes of metadata-version stamp",
565 let minsz = cmp::min(vlen, csz);
566 let version_ok = slice::raw::buf_as_slice(cvbuf, minsz,
567 |buf0| buf0 == encoder::metadata_encoding_version);
569 return Err((format_strbuf!("incompatible metadata \
570 version found: '{}'",
571 filename.display())));
574 let cvbuf1 = cvbuf.offset(vlen as int);
575 debug!("inflating {} bytes of compressed metadata",
577 slice::raw::buf_as_slice(cvbuf1, csz-vlen, |bytes| {
578 match flate::inflate_bytes(bytes) {
579 Some(inflated) => found = Ok(MetadataVec(inflated)),
582 Err(format_strbuf!("failed to decompress \
592 llvm::LLVMMoveToNextSection(si.llsi);
594 return Err(format_strbuf!("metadata not found: '{}'",
595 filename.display()));
599 pub fn meta_section_name(os: Os) -> &'static str {
601 OsMacos => "__DATA,__note.rustc",
602 OsWin32 => ".note.rustc",
603 OsLinux => ".note.rustc",
604 OsAndroid => ".note.rustc",
605 OsFreebsd => ".note.rustc"
609 pub fn read_meta_section_name(os: Os) -> &'static str {
611 OsMacos => "__note.rustc",
612 OsWin32 => ".note.rustc",
613 OsLinux => ".note.rustc",
614 OsAndroid => ".note.rustc",
615 OsFreebsd => ".note.rustc"
619 // A diagnostic function for dumping crate metadata to an output stream
620 pub fn list_file_metadata(os: Os, path: &Path,
621 out: &mut io::Writer) -> io::IoResult<()> {
622 match get_metadata_section(os, path) {
623 Ok(bytes) => decoder::list_crate_metadata(bytes.as_slice(), out),
625 write!(out, "{}\n", msg)