]>
Commit | Line | Data |
---|---|---|
85aaf69f | 1 | // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
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. | |
10 | ||
223e47cc | 11 | //! Finds crate binaries and loads their metadata |
1a4d82fc JJ |
12 | //! |
13 | //! Might I be the first to welcome you to a world of platform differences, | |
14 | //! version requirements, dependency graphs, conflicting desires, and fun! This | |
15 | //! is the major guts (along with metadata::creader) of the compiler for loading | |
16 | //! crates and resolving dependencies. Let's take a tour! | |
17 | //! | |
18 | //! # The problem | |
19 | //! | |
20 | //! Each invocation of the compiler is immediately concerned with one primary | |
21 | //! problem, to connect a set of crates to resolved crates on the filesystem. | |
22 | //! Concretely speaking, the compiler follows roughly these steps to get here: | |
23 | //! | |
24 | //! 1. Discover a set of `extern crate` statements. | |
25 | //! 2. Transform these directives into crate names. If the directive does not | |
26 | //! have an explicit name, then the identifier is the name. | |
27 | //! 3. For each of these crate names, find a corresponding crate on the | |
28 | //! filesystem. | |
29 | //! | |
30 | //! Sounds easy, right? Let's walk into some of the nuances. | |
31 | //! | |
32 | //! ## Transitive Dependencies | |
33 | //! | |
34 | //! Let's say we've got three crates: A, B, and C. A depends on B, and B depends | |
35 | //! on C. When we're compiling A, we primarily need to find and locate B, but we | |
36 | //! also end up needing to find and locate C as well. | |
37 | //! | |
38 | //! The reason for this is that any of B's types could be composed of C's types, | |
39 | //! any function in B could return a type from C, etc. To be able to guarantee | |
40 | //! that we can always typecheck/translate any function, we have to have | |
41 | //! complete knowledge of the whole ecosystem, not just our immediate | |
42 | //! dependencies. | |
43 | //! | |
44 | //! So now as part of the "find a corresponding crate on the filesystem" step | |
45 | //! above, this involves also finding all crates for *all upstream | |
46 | //! dependencies*. This includes all dependencies transitively. | |
47 | //! | |
48 | //! ## Rlibs and Dylibs | |
49 | //! | |
50 | //! The compiler has two forms of intermediate dependencies. These are dubbed | |
51 | //! rlibs and dylibs for the static and dynamic variants, respectively. An rlib | |
52 | //! is a rustc-defined file format (currently just an ar archive) while a dylib | |
53 | //! is a platform-defined dynamic library. Each library has a metadata somewhere | |
54 | //! inside of it. | |
55 | //! | |
56 | //! When translating a crate name to a crate on the filesystem, we all of a | |
57 | //! sudden need to take into account both rlibs and dylibs! Linkage later on may | |
58 | //! use either one of these files, as each has their pros/cons. The job of crate | |
59 | //! loading is to discover what's possible by finding all candidates. | |
60 | //! | |
61 | //! Most parts of this loading systems keep the dylib/rlib as just separate | |
62 | //! variables. | |
63 | //! | |
64 | //! ## Where to look? | |
65 | //! | |
66 | //! We can't exactly scan your whole hard drive when looking for dependencies, | |
67 | //! so we need to places to look. Currently the compiler will implicitly add the | |
68 | //! target lib search path ($prefix/lib/rustlib/$target/lib) to any compilation, | |
69 | //! and otherwise all -L flags are added to the search paths. | |
70 | //! | |
71 | //! ## What criterion to select on? | |
72 | //! | |
73 | //! This a pretty tricky area of loading crates. Given a file, how do we know | |
74 | //! whether it's the right crate? Currently, the rules look along these lines: | |
75 | //! | |
76 | //! 1. Does the filename match an rlib/dylib pattern? That is to say, does the | |
77 | //! filename have the right prefix/suffix? | |
78 | //! 2. Does the filename have the right prefix for the crate name being queried? | |
79 | //! This is filtering for files like `libfoo*.rlib` and such. | |
80 | //! 3. Is the file an actual rust library? This is done by loading the metadata | |
81 | //! from the library and making sure it's actually there. | |
82 | //! 4. Does the name in the metadata agree with the name of the library? | |
83 | //! 5. Does the target in the metadata agree with the current target? | |
84 | //! 6. Does the SVH match? (more on this later) | |
85 | //! | |
86 | //! If the file answers `yes` to all these questions, then the file is | |
87 | //! considered as being *candidate* for being accepted. It is illegal to have | |
88 | //! more than two candidates as the compiler has no method by which to resolve | |
89 | //! this conflict. Additionally, rlib/dylib candidates are considered | |
90 | //! separately. | |
91 | //! | |
92 | //! After all this has happened, we have 1 or two files as candidates. These | |
93 | //! represent the rlib/dylib file found for a library, and they're returned as | |
94 | //! being found. | |
95 | //! | |
96 | //! ### What about versions? | |
97 | //! | |
98 | //! A lot of effort has been put forth to remove versioning from the compiler. | |
99 | //! There have been forays in the past to have versioning baked in, but it was | |
100 | //! largely always deemed insufficient to the point that it was recognized that | |
101 | //! it's probably something the compiler shouldn't do anyway due to its | |
102 | //! complicated nature and the state of the half-baked solutions. | |
103 | //! | |
104 | //! With a departure from versioning, the primary criterion for loading crates | |
105 | //! is just the name of a crate. If we stopped here, it would imply that you | |
106 | //! could never link two crates of the same name from different sources | |
107 | //! together, which is clearly a bad state to be in. | |
108 | //! | |
109 | //! To resolve this problem, we come to the next section! | |
110 | //! | |
111 | //! # Expert Mode | |
112 | //! | |
113 | //! A number of flags have been added to the compiler to solve the "version | |
114 | //! problem" in the previous section, as well as generally enabling more | |
115 | //! powerful usage of the crate loading system of the compiler. The goal of | |
116 | //! these flags and options are to enable third-party tools to drive the | |
117 | //! compiler with prior knowledge about how the world should look. | |
118 | //! | |
119 | //! ## The `--extern` flag | |
120 | //! | |
121 | //! The compiler accepts a flag of this form a number of times: | |
122 | //! | |
123 | //! ```text | |
124 | //! --extern crate-name=path/to/the/crate.rlib | |
125 | //! ``` | |
126 | //! | |
127 | //! This flag is basically the following letter to the compiler: | |
128 | //! | |
129 | //! > Dear rustc, | |
130 | //! > | |
131 | //! > When you are attempting to load the immediate dependency `crate-name`, I | |
9346a6ac | 132 | //! > would like you to assume that the library is located at |
1a4d82fc JJ |
133 | //! > `path/to/the/crate.rlib`, and look nowhere else. Also, please do not |
134 | //! > assume that the path I specified has the name `crate-name`. | |
135 | //! | |
136 | //! This flag basically overrides most matching logic except for validating that | |
137 | //! the file is indeed a rust library. The same `crate-name` can be specified | |
138 | //! twice to specify the rlib/dylib pair. | |
139 | //! | |
140 | //! ## Enabling "multiple versions" | |
141 | //! | |
142 | //! This basically boils down to the ability to specify arbitrary packages to | |
143 | //! the compiler. For example, if crate A wanted to use Bv1 and Bv2, then it | |
144 | //! would look something like: | |
145 | //! | |
146 | //! ```ignore | |
147 | //! extern crate b1; | |
148 | //! extern crate b2; | |
149 | //! | |
150 | //! fn main() {} | |
151 | //! ``` | |
152 | //! | |
153 | //! and the compiler would be invoked as: | |
154 | //! | |
155 | //! ```text | |
156 | //! rustc a.rs --extern b1=path/to/libb1.rlib --extern b2=path/to/libb2.rlib | |
157 | //! ``` | |
158 | //! | |
159 | //! In this scenario there are two crates named `b` and the compiler must be | |
160 | //! manually driven to be informed where each crate is. | |
161 | //! | |
162 | //! ## Frobbing symbols | |
163 | //! | |
164 | //! One of the immediate problems with linking the same library together twice | |
165 | //! in the same problem is dealing with duplicate symbols. The primary way to | |
166 | //! deal with this in rustc is to add hashes to the end of each symbol. | |
167 | //! | |
168 | //! In order to force hashes to change between versions of a library, if | |
169 | //! desired, the compiler exposes an option `-C metadata=foo`, which is used to | |
170 | //! initially seed each symbol hash. The string `foo` is prepended to each | |
171 | //! string-to-hash to ensure that symbols change over time. | |
172 | //! | |
173 | //! ## Loading transitive dependencies | |
174 | //! | |
175 | //! Dealing with same-named-but-distinct crates is not just a local problem, but | |
176 | //! one that also needs to be dealt with for transitive dependencies. Note that | |
177 | //! in the letter above `--extern` flags only apply to the *local* set of | |
178 | //! dependencies, not the upstream transitive dependencies. Consider this | |
179 | //! dependency graph: | |
180 | //! | |
181 | //! ```text | |
182 | //! A.1 A.2 | |
183 | //! | | | |
184 | //! | | | |
185 | //! B C | |
186 | //! \ / | |
187 | //! \ / | |
188 | //! D | |
189 | //! ``` | |
190 | //! | |
191 | //! In this scenario, when we compile `D`, we need to be able to distinctly | |
192 | //! resolve `A.1` and `A.2`, but an `--extern` flag cannot apply to these | |
193 | //! transitive dependencies. | |
194 | //! | |
195 | //! Note that the key idea here is that `B` and `C` are both *already compiled*. | |
196 | //! That is, they have already resolved their dependencies. Due to unrelated | |
197 | //! technical reasons, when a library is compiled, it is only compatible with | |
198 | //! the *exact same* version of the upstream libraries it was compiled against. | |
199 | //! We use the "Strict Version Hash" to identify the exact copy of an upstream | |
200 | //! library. | |
201 | //! | |
202 | //! With this knowledge, we know that `B` and `C` will depend on `A` with | |
203 | //! different SVH values, so we crawl the normal `-L` paths looking for | |
204 | //! `liba*.rlib` and filter based on the contained SVH. | |
205 | //! | |
206 | //! In the end, this ends up not needing `--extern` to specify upstream | |
207 | //! transitive dependencies. | |
208 | //! | |
209 | //! # Wrapping up | |
210 | //! | |
211 | //! That's the general overview of loading crates in the compiler, but it's by | |
212 | //! no means all of the necessary details. Take a look at the rest of | |
213 | //! metadata::loader or metadata::creader for all the juicy details! | |
223e47cc | 214 | |
92a42be0 SL |
215 | use cstore::{MetadataBlob, MetadataVec, MetadataArchive}; |
216 | use decoder; | |
217 | use encoder; | |
218 | ||
219 | use rustc::back::svh::Svh; | |
220 | use rustc::session::Session; | |
221 | use rustc::session::filesearch::{FileSearch, FileMatches, FileDoesntMatch}; | |
222 | use rustc::session::search_paths::PathKind; | |
223 | use rustc::util::common; | |
224 | ||
225 | use rustc_llvm as llvm; | |
226 | use rustc_llvm::{False, ObjectFile, mk_section_iter}; | |
227 | use rustc_llvm::archive_ro::ArchiveRO; | |
1a4d82fc JJ |
228 | use syntax::codemap::Span; |
229 | use syntax::diagnostic::SpanHandler; | |
85aaf69f | 230 | use rustc_back::target::Target; |
1a4d82fc | 231 | |
1a4d82fc | 232 | use std::cmp; |
85aaf69f | 233 | use std::collections::HashMap; |
d9579d0f | 234 | use std::fs; |
c34b1796 AL |
235 | use std::io::prelude::*; |
236 | use std::io; | |
237 | use std::path::{Path, PathBuf}; | |
970d7e83 | 238 | use std::ptr; |
1a4d82fc | 239 | use std::slice; |
92a42be0 | 240 | use std::time::Instant; |
1a4d82fc JJ |
241 | |
242 | use flate; | |
243 | ||
244 | pub struct CrateMismatch { | |
c34b1796 | 245 | path: PathBuf, |
1a4d82fc | 246 | got: String, |
223e47cc LB |
247 | } |
248 | ||
1a4d82fc JJ |
249 | pub struct Context<'a> { |
250 | pub sess: &'a Session, | |
251 | pub span: Span, | |
252 | pub ident: &'a str, | |
253 | pub crate_name: &'a str, | |
254 | pub hash: Option<&'a Svh>, | |
85aaf69f SL |
255 | // points to either self.sess.target.target or self.sess.host, must match triple |
256 | pub target: &'a Target, | |
1a4d82fc JJ |
257 | pub triple: &'a str, |
258 | pub filesearch: FileSearch<'a>, | |
259 | pub root: &'a Option<CratePaths>, | |
260 | pub rejected_via_hash: Vec<CrateMismatch>, | |
261 | pub rejected_via_triple: Vec<CrateMismatch>, | |
85aaf69f | 262 | pub rejected_via_kind: Vec<CrateMismatch>, |
1a4d82fc | 263 | pub should_match_name: bool, |
223e47cc LB |
264 | } |
265 | ||
1a4d82fc | 266 | pub struct Library { |
c34b1796 AL |
267 | pub dylib: Option<(PathBuf, PathKind)>, |
268 | pub rlib: Option<(PathBuf, PathKind)>, | |
1a4d82fc | 269 | pub metadata: MetadataBlob, |
223e47cc LB |
270 | } |
271 | ||
1a4d82fc JJ |
272 | pub struct ArchiveMetadata { |
273 | _archive: ArchiveRO, | |
274 | // points into self._archive | |
275 | data: *const [u8], | |
223e47cc LB |
276 | } |
277 | ||
1a4d82fc JJ |
278 | pub struct CratePaths { |
279 | pub ident: String, | |
c34b1796 AL |
280 | pub dylib: Option<PathBuf>, |
281 | pub rlib: Option<PathBuf> | |
223e47cc LB |
282 | } |
283 | ||
c1a9b12d SL |
284 | pub const METADATA_FILENAME: &'static str = "rust.metadata.bin"; |
285 | ||
1a4d82fc | 286 | impl CratePaths { |
c34b1796 | 287 | fn paths(&self) -> Vec<PathBuf> { |
1a4d82fc JJ |
288 | match (&self.dylib, &self.rlib) { |
289 | (&None, &None) => vec!(), | |
290 | (&Some(ref p), &None) | | |
291 | (&None, &Some(ref p)) => vec!(p.clone()), | |
292 | (&Some(ref p1), &Some(ref p2)) => vec!(p1.clone(), p2.clone()), | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
297 | impl<'a> Context<'a> { | |
298 | pub fn maybe_load_library_crate(&mut self) -> Option<Library> { | |
299 | self.find_library_crate() | |
300 | } | |
301 | ||
302 | pub fn load_library_crate(&mut self) -> Library { | |
303 | match self.find_library_crate() { | |
304 | Some(t) => t, | |
305 | None => { | |
306 | self.report_load_errs(); | |
307 | unreachable!() | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | pub fn report_load_errs(&mut self) { | |
b039eaaf SL |
313 | let add = match self.root { |
314 | &None => String::new(), | |
315 | &Some(ref r) => format!(" which `{}` depends on", | |
316 | r.ident) | |
317 | }; | |
318 | if !self.rejected_via_hash.is_empty() { | |
319 | span_err!(self.sess, self.span, E0460, | |
320 | "found possibly newer version of crate `{}`{}", | |
321 | self.ident, add); | |
9346a6ac | 322 | } else if !self.rejected_via_triple.is_empty() { |
b039eaaf SL |
323 | span_err!(self.sess, self.span, E0461, |
324 | "couldn't find crate `{}` with expected target triple {}{}", | |
325 | self.ident, self.triple, add); | |
9346a6ac | 326 | } else if !self.rejected_via_kind.is_empty() { |
b039eaaf SL |
327 | span_err!(self.sess, self.span, E0462, |
328 | "found staticlib `{}` instead of rlib or dylib{}", | |
329 | self.ident, add); | |
1a4d82fc | 330 | } else { |
b039eaaf SL |
331 | span_err!(self.sess, self.span, E0463, |
332 | "can't find crate for `{}`{}", | |
333 | self.ident, add); | |
334 | } | |
1a4d82fc | 335 | |
9346a6ac | 336 | if !self.rejected_via_triple.is_empty() { |
1a4d82fc JJ |
337 | let mismatches = self.rejected_via_triple.iter(); |
338 | for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() { | |
339 | self.sess.fileline_note(self.span, | |
340 | &format!("crate `{}`, path #{}, triple {}: {}", | |
c34b1796 | 341 | self.ident, i+1, got, path.display())); |
1a4d82fc JJ |
342 | } |
343 | } | |
9346a6ac | 344 | if !self.rejected_via_hash.is_empty() { |
1a4d82fc JJ |
345 | self.sess.span_note(self.span, "perhaps this crate needs \ |
346 | to be recompiled?"); | |
347 | let mismatches = self.rejected_via_hash.iter(); | |
348 | for (i, &CrateMismatch{ ref path, .. }) in mismatches.enumerate() { | |
349 | self.sess.fileline_note(self.span, | |
85aaf69f | 350 | &format!("crate `{}` path #{}: {}", |
c34b1796 | 351 | self.ident, i+1, path.display())); |
1a4d82fc JJ |
352 | } |
353 | match self.root { | |
354 | &None => {} | |
355 | &Some(ref r) => { | |
356 | for (i, path) in r.paths().iter().enumerate() { | |
357 | self.sess.fileline_note(self.span, | |
358 | &format!("crate `{}` path #{}: {}", | |
c34b1796 | 359 | r.ident, i+1, path.display())); |
970d7e83 | 360 | } |
223e47cc | 361 | } |
223e47cc | 362 | } |
1a4d82fc | 363 | } |
9346a6ac | 364 | if !self.rejected_via_kind.is_empty() { |
c34b1796 | 365 | self.sess.fileline_help(self.span, "please recompile this crate using \ |
85aaf69f SL |
366 | --crate-type lib"); |
367 | let mismatches = self.rejected_via_kind.iter(); | |
368 | for (i, &CrateMismatch { ref path, .. }) in mismatches.enumerate() { | |
369 | self.sess.fileline_note(self.span, | |
370 | &format!("crate `{}` path #{}: {}", | |
c34b1796 | 371 | self.ident, i+1, path.display())); |
85aaf69f SL |
372 | } |
373 | } | |
1a4d82fc JJ |
374 | self.sess.abort_if_errors(); |
375 | } | |
376 | ||
377 | fn find_library_crate(&mut self) -> Option<Library> { | |
378 | // If an SVH is specified, then this is a transitive dependency that | |
379 | // must be loaded via -L plus some filtering. | |
380 | if self.hash.is_none() { | |
381 | self.should_match_name = false; | |
85aaf69f SL |
382 | if let Some(s) = self.sess.opts.externs.get(self.crate_name) { |
383 | return self.find_commandline_library(s); | |
1a4d82fc JJ |
384 | } |
385 | self.should_match_name = true; | |
386 | } | |
387 | ||
388 | let dypair = self.dylibname(); | |
389 | ||
390 | // want: crate_name.dir_part() + prefix + crate_name.file_part + "-" | |
391 | let dylib_prefix = format!("{}{}", dypair.0, self.crate_name); | |
392 | let rlib_prefix = format!("lib{}", self.crate_name); | |
85aaf69f | 393 | let staticlib_prefix = format!("lib{}", self.crate_name); |
1a4d82fc JJ |
394 | |
395 | let mut candidates = HashMap::new(); | |
85aaf69f | 396 | let mut staticlibs = vec!(); |
1a4d82fc JJ |
397 | |
398 | // First, find all possible candidate rlibs and dylibs purely based on | |
399 | // the name of the files themselves. We're trying to match against an | |
400 | // exact crate name and a possibly an exact hash. | |
401 | // | |
402 | // During this step, we can filter all found libraries based on the | |
403 | // name and id found in the crate id (we ignore the path portion for | |
404 | // filename matching), as well as the exact hash (if specified). If we | |
405 | // end up having many candidates, we must look at the metadata to | |
406 | // perform exact matches against hashes/crate ids. Note that opening up | |
407 | // the metadata is where we do an exact match against the full contents | |
408 | // of the crate id (path/name/id). | |
409 | // | |
410 | // The goal of this step is to look at as little metadata as possible. | |
85aaf69f | 411 | self.filesearch.search(|path, kind| { |
c34b1796 | 412 | let file = match path.file_name().and_then(|s| s.to_str()) { |
1a4d82fc JJ |
413 | None => return FileDoesntMatch, |
414 | Some(file) => file, | |
415 | }; | |
85aaf69f SL |
416 | let (hash, rlib) = if file.starts_with(&rlib_prefix[..]) && |
417 | file.ends_with(".rlib") { | |
418 | (&file[(rlib_prefix.len()) .. (file.len() - ".rlib".len())], | |
1a4d82fc | 419 | true) |
85aaf69f SL |
420 | } else if file.starts_with(&dylib_prefix) && |
421 | file.ends_with(&dypair.1) { | |
422 | (&file[(dylib_prefix.len()) .. (file.len() - dypair.1.len())], | |
1a4d82fc JJ |
423 | false) |
424 | } else { | |
85aaf69f SL |
425 | if file.starts_with(&staticlib_prefix[..]) && |
426 | file.ends_with(".a") { | |
427 | staticlibs.push(CrateMismatch { | |
c34b1796 | 428 | path: path.to_path_buf(), |
85aaf69f SL |
429 | got: "static".to_string() |
430 | }); | |
431 | } | |
1a4d82fc JJ |
432 | return FileDoesntMatch |
433 | }; | |
434 | info!("lib candidate: {}", path.display()); | |
435 | ||
436 | let hash_str = hash.to_string(); | |
c34b1796 AL |
437 | let slot = candidates.entry(hash_str) |
438 | .or_insert_with(|| (HashMap::new(), HashMap::new())); | |
1a4d82fc | 439 | let (ref mut rlibs, ref mut dylibs) = *slot; |
62682a34 SL |
440 | fs::canonicalize(path).map(|p| { |
441 | if rlib { | |
442 | rlibs.insert(p, kind); | |
443 | } else { | |
444 | dylibs.insert(p, kind); | |
445 | } | |
446 | FileMatches | |
447 | }).unwrap_or(FileDoesntMatch) | |
1a4d82fc | 448 | }); |
62682a34 | 449 | self.rejected_via_kind.extend(staticlibs); |
1a4d82fc JJ |
450 | |
451 | // We have now collected all known libraries into a set of candidates | |
452 | // keyed of the filename hash listed. For each filename, we also have a | |
453 | // list of rlibs/dylibs that apply. Here, we map each of these lists | |
454 | // (per hash), to a Library candidate for returning. | |
455 | // | |
456 | // A Library candidate is created if the metadata for the set of | |
457 | // libraries corresponds to the crate id and hash criteria that this | |
458 | // search is being performed for. | |
459 | let mut libraries = Vec::new(); | |
85aaf69f | 460 | for (_hash, (rlibs, dylibs)) in candidates { |
1a4d82fc JJ |
461 | let mut metadata = None; |
462 | let rlib = self.extract_one(rlibs, "rlib", &mut metadata); | |
463 | let dylib = self.extract_one(dylibs, "dylib", &mut metadata); | |
464 | match metadata { | |
465 | Some(metadata) => { | |
466 | libraries.push(Library { | |
467 | dylib: dylib, | |
468 | rlib: rlib, | |
469 | metadata: metadata, | |
470 | }) | |
471 | } | |
472 | None => {} | |
473 | } | |
474 | } | |
475 | ||
476 | // Having now translated all relevant found hashes into libraries, see | |
477 | // what we've got and figure out if we found multiple candidates for | |
478 | // libraries or not. | |
479 | match libraries.len() { | |
480 | 0 => None, | |
481 | 1 => Some(libraries.into_iter().next().unwrap()), | |
970d7e83 | 482 | _ => { |
b039eaaf SL |
483 | span_err!(self.sess, self.span, E0464, |
484 | "multiple matching crates for `{}`", | |
485 | self.crate_name); | |
1a4d82fc | 486 | self.sess.note("candidates:"); |
85aaf69f | 487 | for lib in &libraries { |
1a4d82fc | 488 | match lib.dylib { |
85aaf69f | 489 | Some((ref p, _)) => { |
1a4d82fc | 490 | self.sess.note(&format!("path: {}", |
c34b1796 | 491 | p.display())); |
1a4d82fc JJ |
492 | } |
493 | None => {} | |
494 | } | |
495 | match lib.rlib { | |
85aaf69f | 496 | Some((ref p, _)) => { |
1a4d82fc | 497 | self.sess.note(&format!("path: {}", |
c34b1796 | 498 | p.display())); |
1a4d82fc JJ |
499 | } |
500 | None => {} | |
501 | } | |
502 | let data = lib.metadata.as_slice(); | |
503 | let name = decoder::get_crate_name(data); | |
c34b1796 | 504 | note_crate_name(self.sess.diagnostic(), &name); |
1a4d82fc | 505 | } |
970d7e83 LB |
506 | None |
507 | } | |
1a4d82fc JJ |
508 | } |
509 | } | |
510 | ||
511 | // Attempts to extract *one* library from the set `m`. If the set has no | |
512 | // elements, `None` is returned. If the set has more than one element, then | |
513 | // the errors and notes are emitted about the set of libraries. | |
514 | // | |
515 | // With only one library in the set, this function will extract it, and then | |
516 | // read the metadata from it if `*slot` is `None`. If the metadata couldn't | |
517 | // be read, it is assumed that the file isn't a valid rust library (no | |
518 | // errors are emitted). | |
c34b1796 AL |
519 | fn extract_one(&mut self, m: HashMap<PathBuf, PathKind>, flavor: &str, |
520 | slot: &mut Option<MetadataBlob>) -> Option<(PathBuf, PathKind)> { | |
521 | let mut ret = None::<(PathBuf, PathKind)>; | |
85aaf69f | 522 | let mut error = 0; |
1a4d82fc JJ |
523 | |
524 | if slot.is_some() { | |
525 | // FIXME(#10786): for an optimization, we only read one of the | |
526 | // library's metadata sections. In theory we should | |
527 | // read both, but reading dylib metadata is quite | |
528 | // slow. | |
9346a6ac | 529 | if m.is_empty() { |
1a4d82fc JJ |
530 | return None |
531 | } else if m.len() == 1 { | |
532 | return Some(m.into_iter().next().unwrap()) | |
533 | } | |
534 | } | |
535 | ||
85aaf69f | 536 | for (lib, kind) in m { |
1a4d82fc | 537 | info!("{} reading metadata from: {}", flavor, lib.display()); |
62682a34 | 538 | let metadata = match get_metadata_section(self.target, &lib) { |
1a4d82fc JJ |
539 | Ok(blob) => { |
540 | if self.crate_matches(blob.as_slice(), &lib) { | |
541 | blob | |
542 | } else { | |
543 | info!("metadata mismatch"); | |
544 | continue | |
545 | } | |
970d7e83 | 546 | } |
92a42be0 SL |
547 | Err(err) => { |
548 | info!("no metadata found: {}", err); | |
1a4d82fc JJ |
549 | continue |
550 | } | |
551 | }; | |
92a42be0 SL |
552 | // If we've already found a candidate and we're not matching hashes, |
553 | // emit an error about duplicate candidates found. If we're matching | |
554 | // based on a hash, however, then if we've gotten this far both | |
555 | // candidates have the same hash, so they're not actually | |
556 | // duplicates that we should warn about. | |
557 | if ret.is_some() && self.hash.is_none() { | |
b039eaaf SL |
558 | span_err!(self.sess, self.span, E0465, |
559 | "multiple {} candidates for `{}` found", | |
560 | flavor, self.crate_name); | |
1a4d82fc JJ |
561 | self.sess.span_note(self.span, |
562 | &format!(r"candidate #1: {}", | |
85aaf69f | 563 | ret.as_ref().unwrap().0 |
c34b1796 | 564 | .display())); |
1a4d82fc JJ |
565 | error = 1; |
566 | ret = None; | |
567 | } | |
568 | if error > 0 { | |
569 | error += 1; | |
570 | self.sess.span_note(self.span, | |
571 | &format!(r"candidate #{}: {}", error, | |
c34b1796 | 572 | lib.display())); |
1a4d82fc | 573 | continue |
970d7e83 | 574 | } |
1a4d82fc | 575 | *slot = Some(metadata); |
85aaf69f | 576 | ret = Some((lib, kind)); |
1a4d82fc JJ |
577 | } |
578 | return if error > 0 {None} else {ret} | |
579 | } | |
580 | ||
581 | fn crate_matches(&mut self, crate_data: &[u8], libpath: &Path) -> bool { | |
582 | if self.should_match_name { | |
583 | match decoder::maybe_get_crate_name(crate_data) { | |
584 | Some(ref name) if self.crate_name == *name => {} | |
585 | _ => { info!("Rejecting via crate name"); return false } | |
586 | } | |
587 | } | |
588 | let hash = match decoder::maybe_get_crate_hash(crate_data) { | |
589 | Some(hash) => hash, None => { | |
590 | info!("Rejecting via lack of crate hash"); | |
591 | return false; | |
592 | } | |
593 | }; | |
594 | ||
595 | let triple = match decoder::get_crate_triple(crate_data) { | |
596 | None => { debug!("triple not present"); return false } | |
597 | Some(t) => t, | |
598 | }; | |
599 | if triple != self.triple { | |
600 | info!("Rejecting via crate triple: expected {} got {}", self.triple, triple); | |
601 | self.rejected_via_triple.push(CrateMismatch { | |
c34b1796 | 602 | path: libpath.to_path_buf(), |
1a4d82fc JJ |
603 | got: triple.to_string() |
604 | }); | |
605 | return false; | |
223e47cc | 606 | } |
223e47cc | 607 | |
1a4d82fc JJ |
608 | match self.hash { |
609 | None => true, | |
610 | Some(myhash) => { | |
611 | if *myhash != hash { | |
612 | info!("Rejecting via hash: expected {} got {}", *myhash, hash); | |
613 | self.rejected_via_hash.push(CrateMismatch { | |
c34b1796 | 614 | path: libpath.to_path_buf(), |
1a4d82fc JJ |
615 | got: myhash.as_str().to_string() |
616 | }); | |
617 | false | |
618 | } else { | |
619 | true | |
620 | } | |
621 | } | |
223e47cc | 622 | } |
223e47cc | 623 | } |
223e47cc | 624 | |
1a4d82fc JJ |
625 | |
626 | // Returns the corresponding (prefix, suffix) that files need to have for | |
627 | // dynamic libraries | |
628 | fn dylibname(&self) -> (String, String) { | |
85aaf69f | 629 | let t = &self.target; |
1a4d82fc | 630 | (t.options.dll_prefix.clone(), t.options.dll_suffix.clone()) |
223e47cc | 631 | } |
223e47cc | 632 | |
85aaf69f | 633 | fn find_commandline_library(&mut self, locs: &[String]) -> Option<Library> { |
1a4d82fc JJ |
634 | // First, filter out all libraries that look suspicious. We only accept |
635 | // files which actually exist that have the correct naming scheme for | |
636 | // rlibs/dylibs. | |
637 | let sess = self.sess; | |
638 | let dylibname = self.dylibname(); | |
85aaf69f SL |
639 | let mut rlibs = HashMap::new(); |
640 | let mut dylibs = HashMap::new(); | |
1a4d82fc | 641 | { |
c34b1796 | 642 | let locs = locs.iter().map(|l| PathBuf::from(l)).filter(|loc| { |
1a4d82fc JJ |
643 | if !loc.exists() { |
644 | sess.err(&format!("extern location for {} does not exist: {}", | |
c34b1796 | 645 | self.crate_name, loc.display())); |
1a4d82fc JJ |
646 | return false; |
647 | } | |
c34b1796 | 648 | let file = match loc.file_name().and_then(|s| s.to_str()) { |
1a4d82fc JJ |
649 | Some(file) => file, |
650 | None => { | |
651 | sess.err(&format!("extern location for {} is not a file: {}", | |
c34b1796 | 652 | self.crate_name, loc.display())); |
1a4d82fc JJ |
653 | return false; |
654 | } | |
655 | }; | |
656 | if file.starts_with("lib") && file.ends_with(".rlib") { | |
657 | return true | |
658 | } else { | |
659 | let (ref prefix, ref suffix) = dylibname; | |
85aaf69f SL |
660 | if file.starts_with(&prefix[..]) && |
661 | file.ends_with(&suffix[..]) { | |
1a4d82fc JJ |
662 | return true |
663 | } | |
664 | } | |
665 | sess.err(&format!("extern location for {} is of an unknown type: {}", | |
c34b1796 | 666 | self.crate_name, loc.display())); |
1a4d82fc JJ |
667 | false |
668 | }); | |
669 | ||
85aaf69f SL |
670 | // Now that we have an iterator of good candidates, make sure |
671 | // there's at most one rlib and at most one dylib. | |
1a4d82fc | 672 | for loc in locs { |
c34b1796 | 673 | if loc.file_name().unwrap().to_str().unwrap().ends_with(".rlib") { |
d9579d0f | 674 | rlibs.insert(fs::canonicalize(&loc).unwrap(), |
85aaf69f | 675 | PathKind::ExternFlag); |
1a4d82fc | 676 | } else { |
d9579d0f | 677 | dylibs.insert(fs::canonicalize(&loc).unwrap(), |
85aaf69f | 678 | PathKind::ExternFlag); |
1a4d82fc JJ |
679 | } |
680 | } | |
681 | }; | |
682 | ||
683 | // Extract the rlib/dylib pair. | |
684 | let mut metadata = None; | |
685 | let rlib = self.extract_one(rlibs, "rlib", &mut metadata); | |
686 | let dylib = self.extract_one(dylibs, "dylib", &mut metadata); | |
687 | ||
688 | if rlib.is_none() && dylib.is_none() { return None } | |
689 | match metadata { | |
690 | Some(metadata) => Some(Library { | |
691 | dylib: dylib, | |
692 | rlib: rlib, | |
693 | metadata: metadata, | |
694 | }), | |
695 | None => None, | |
696 | } | |
223e47cc | 697 | } |
223e47cc LB |
698 | } |
699 | ||
1a4d82fc | 700 | pub fn note_crate_name(diag: &SpanHandler, name: &str) { |
c34b1796 | 701 | diag.handler().note(&format!("crate name: {}", name)); |
1a4d82fc | 702 | } |
223e47cc | 703 | |
1a4d82fc JJ |
704 | impl ArchiveMetadata { |
705 | fn new(ar: ArchiveRO) -> Option<ArchiveMetadata> { | |
d9579d0f AL |
706 | let data = { |
707 | let section = ar.iter().find(|sect| { | |
708 | sect.name() == Some(METADATA_FILENAME) | |
709 | }); | |
710 | match section { | |
711 | Some(s) => s.data() as *const [u8], | |
712 | None => { | |
713 | debug!("didn't find '{}' in the archive", METADATA_FILENAME); | |
714 | return None; | |
715 | } | |
1a4d82fc JJ |
716 | } |
717 | }; | |
223e47cc | 718 | |
1a4d82fc JJ |
719 | Some(ArchiveMetadata { |
720 | _archive: ar, | |
721 | data: data, | |
722 | }) | |
223e47cc | 723 | } |
1a4d82fc JJ |
724 | |
725 | pub fn as_slice<'a>(&'a self) -> &'a [u8] { unsafe { &*self.data } } | |
726 | } | |
727 | ||
728 | // Just a small wrapper to time how long reading metadata takes. | |
62682a34 SL |
729 | fn get_metadata_section(target: &Target, filename: &Path) |
730 | -> Result<MetadataBlob, String> { | |
92a42be0 SL |
731 | let start = Instant::now(); |
732 | let ret = get_metadata_section_imp(target, filename); | |
733 | info!("reading {:?} => {:?}", filename.file_name().unwrap(), | |
734 | start.elapsed()); | |
735 | return ret | |
223e47cc LB |
736 | } |
737 | ||
62682a34 SL |
738 | fn get_metadata_section_imp(target: &Target, filename: &Path) |
739 | -> Result<MetadataBlob, String> { | |
1a4d82fc JJ |
740 | if !filename.exists() { |
741 | return Err(format!("no such file: '{}'", filename.display())); | |
742 | } | |
c34b1796 | 743 | if filename.file_name().unwrap().to_str().unwrap().ends_with(".rlib") { |
1a4d82fc JJ |
744 | // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap |
745 | // internally to read the file. We also avoid even using a memcpy by | |
746 | // just keeping the archive along while the metadata is in use. | |
747 | let archive = match ArchiveRO::open(filename) { | |
748 | Some(ar) => ar, | |
749 | None => { | |
750 | debug!("llvm didn't like `{}`", filename.display()); | |
751 | return Err(format!("failed to read rlib metadata: '{}'", | |
752 | filename.display())); | |
753 | } | |
754 | }; | |
755 | return match ArchiveMetadata::new(archive).map(|ar| MetadataArchive(ar)) { | |
c34b1796 AL |
756 | None => Err(format!("failed to read rlib metadata: '{}'", |
757 | filename.display())), | |
758 | Some(blob) => Ok(blob) | |
759 | }; | |
1a4d82fc | 760 | } |
223e47cc | 761 | unsafe { |
c34b1796 | 762 | let buf = common::path2cstr(filename); |
1a4d82fc | 763 | let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr()); |
c34b1796 | 764 | if mb as isize == 0 { |
1a4d82fc JJ |
765 | return Err(format!("error reading library: '{}'", |
766 | filename.display())) | |
767 | } | |
768 | let of = match ObjectFile::new(mb) { | |
769 | Some(of) => of, | |
770 | _ => { | |
771 | return Err((format!("provided path not an object file: '{}'", | |
772 | filename.display()))) | |
773 | } | |
223e47cc LB |
774 | }; |
775 | let si = mk_section_iter(of.llof); | |
776 | while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False { | |
1a4d82fc JJ |
777 | let mut name_buf = ptr::null(); |
778 | let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf); | |
85aaf69f | 779 | let name = slice::from_raw_parts(name_buf as *const u8, |
c34b1796 | 780 | name_len as usize).to_vec(); |
1a4d82fc JJ |
781 | let name = String::from_utf8(name).unwrap(); |
782 | debug!("get_metadata_section: name {}", name); | |
62682a34 | 783 | if read_meta_section_name(target) == name { |
223e47cc | 784 | let cbuf = llvm::LLVMGetSectionContents(si.llsi); |
c34b1796 | 785 | let csz = llvm::LLVMGetSectionSize(si.llsi) as usize; |
1a4d82fc | 786 | let cvbuf: *const u8 = cbuf as *const u8; |
970d7e83 | 787 | let vlen = encoder::metadata_encoding_version.len(); |
1a4d82fc | 788 | debug!("checking {} bytes of metadata-version stamp", |
970d7e83 | 789 | vlen); |
1a4d82fc | 790 | let minsz = cmp::min(vlen, csz); |
85aaf69f | 791 | let buf0 = slice::from_raw_parts(cvbuf, minsz); |
1a4d82fc JJ |
792 | let version_ok = buf0 == encoder::metadata_encoding_version; |
793 | if !version_ok { | |
794 | return Err((format!("incompatible metadata version found: '{}'", | |
795 | filename.display()))); | |
970d7e83 | 796 | } |
223e47cc | 797 | |
c34b1796 | 798 | let cvbuf1 = cvbuf.offset(vlen as isize); |
1a4d82fc | 799 | debug!("inflating {} bytes of compressed metadata", |
970d7e83 | 800 | csz - vlen); |
85aaf69f | 801 | let bytes = slice::from_raw_parts(cvbuf1, csz - vlen); |
1a4d82fc | 802 | match flate::inflate_bytes(bytes) { |
c34b1796 AL |
803 | Ok(inflated) => return Ok(MetadataVec(inflated)), |
804 | Err(_) => {} | |
223e47cc LB |
805 | } |
806 | } | |
807 | llvm::LLVMMoveToNextSection(si.llsi); | |
808 | } | |
c34b1796 | 809 | Err(format!("metadata not found: '{}'", filename.display())) |
223e47cc LB |
810 | } |
811 | } | |
812 | ||
62682a34 SL |
813 | pub fn meta_section_name(target: &Target) -> &'static str { |
814 | if target.options.is_like_osx { | |
1a4d82fc | 815 | "__DATA,__note.rustc" |
62682a34 SL |
816 | } else if target.options.is_like_msvc { |
817 | // When using link.exe it was seen that the section name `.note.rustc` | |
818 | // was getting shortened to `.note.ru`, and according to the PE and COFF | |
819 | // specification: | |
820 | // | |
821 | // > Executable images do not use a string table and do not support | |
822 | // > section names longer than 8 characters | |
823 | // | |
824 | // https://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx | |
825 | // | |
826 | // As a result, we choose a slightly shorter name! As to why | |
827 | // `.note.rustc` works on MinGW, that's another good question... | |
828 | ".rustc" | |
1a4d82fc JJ |
829 | } else { |
830 | ".note.rustc" | |
223e47cc LB |
831 | } |
832 | } | |
833 | ||
62682a34 SL |
834 | pub fn read_meta_section_name(target: &Target) -> &'static str { |
835 | if target.options.is_like_osx { | |
1a4d82fc | 836 | "__note.rustc" |
62682a34 SL |
837 | } else if target.options.is_like_msvc { |
838 | ".rustc" | |
1a4d82fc JJ |
839 | } else { |
840 | ".note.rustc" | |
970d7e83 LB |
841 | } |
842 | } | |
843 | ||
223e47cc | 844 | // A diagnostic function for dumping crate metadata to an output stream |
62682a34 | 845 | pub fn list_file_metadata(target: &Target, path: &Path, |
c34b1796 | 846 | out: &mut io::Write) -> io::Result<()> { |
62682a34 | 847 | match get_metadata_section(target, path) { |
1a4d82fc JJ |
848 | Ok(bytes) => decoder::list_crate_metadata(bytes.as_slice(), out), |
849 | Err(msg) => { | |
850 | write!(out, "{}\n", msg) | |
851 | } | |
223e47cc LB |
852 | } |
853 | } |