]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/back/link.rs
21bc61593c9cdbf451fadcb726ba0277e753be5d
[rust.git] / src / librustc_trans / back / link.rs
1 // Copyright 2012-2014 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.
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
11 use super::archive::{Archive, ArchiveBuilder, ArchiveConfig, METADATA_FILENAME};
12 use super::linker::{Linker, GnuLinker, MsvcLinker};
13 use super::rpath::RPathConfig;
14 use super::rpath;
15 use super::msvc;
16 use super::svh::Svh;
17 use session::config;
18 use session::config::NoDebugInfo;
19 use session::config::{OutputFilenames, Input, OutputTypeBitcode, OutputTypeExe, OutputTypeObject};
20 use session::search_paths::PathKind;
21 use session::Session;
22 use metadata::common::LinkMeta;
23 use metadata::{encoder, cstore, filesearch, csearch, creader};
24 use metadata::filesearch::FileDoesntMatch;
25 use middle::ty::{self, Ty};
26 use rustc::ast_map::{PathElem, PathElems, PathName};
27 use trans::{CrateContext, CrateTranslation, gensym_name};
28 use util::common::time;
29 use util::sha2::{Digest, Sha256};
30 use util::fs::fix_windows_verbatim_for_gcc;
31 use rustc_back::tempdir::TempDir;
32
33 use std::env;
34 use std::ffi::OsString;
35 use std::fs::{self, PathExt};
36 use std::io::{self, Read, Write};
37 use std::mem;
38 use std::path::{Path, PathBuf};
39 use std::process::Command;
40 use std::str;
41 use flate;
42 use serialize::hex::ToHex;
43 use syntax::ast;
44 use syntax::attr::AttrMetaMethods;
45 use syntax::codemap::Span;
46 use syntax::parse::token;
47
48 // RLIB LLVM-BYTECODE OBJECT LAYOUT
49 // Version 1
50 // Bytes    Data
51 // 0..10    "RUST_OBJECT" encoded in ASCII
52 // 11..14   format version as little-endian u32
53 // 15..22   size in bytes of deflate compressed LLVM bitcode as
54 //          little-endian u64
55 // 23..     compressed LLVM bitcode
56
57 // This is the "magic number" expected at the beginning of a LLVM bytecode
58 // object in an rlib.
59 pub const RLIB_BYTECODE_OBJECT_MAGIC: &'static [u8] = b"RUST_OBJECT";
60
61 // The version number this compiler will write to bytecode objects in rlibs
62 pub const RLIB_BYTECODE_OBJECT_VERSION: u32 = 1;
63
64 // The offset in bytes the bytecode object format version number can be found at
65 pub const RLIB_BYTECODE_OBJECT_VERSION_OFFSET: usize = 11;
66
67 // The offset in bytes the size of the compressed bytecode can be found at in
68 // format version 1
69 pub const RLIB_BYTECODE_OBJECT_V1_DATASIZE_OFFSET: usize =
70     RLIB_BYTECODE_OBJECT_VERSION_OFFSET + 4;
71
72 // The offset in bytes the compressed LLVM bytecode can be found at in format
73 // version 1
74 pub const RLIB_BYTECODE_OBJECT_V1_DATA_OFFSET: usize =
75     RLIB_BYTECODE_OBJECT_V1_DATASIZE_OFFSET + 8;
76
77
78 /*
79  * Name mangling and its relationship to metadata. This is complex. Read
80  * carefully.
81  *
82  * The semantic model of Rust linkage is, broadly, that "there's no global
83  * namespace" between crates. Our aim is to preserve the illusion of this
84  * model despite the fact that it's not *quite* possible to implement on
85  * modern linkers. We initially didn't use system linkers at all, but have
86  * been convinced of their utility.
87  *
88  * There are a few issues to handle:
89  *
90  *  - Linkers operate on a flat namespace, so we have to flatten names.
91  *    We do this using the C++ namespace-mangling technique. Foo::bar
92  *    symbols and such.
93  *
94  *  - Symbols with the same name but different types need to get different
95  *    linkage-names. We do this by hashing a string-encoding of the type into
96  *    a fixed-size (currently 16-byte hex) cryptographic hash function (CHF:
97  *    we use SHA256) to "prevent collisions". This is not airtight but 16 hex
98  *    digits on uniform probability means you're going to need 2**32 same-name
99  *    symbols in the same process before you're even hitting birthday-paradox
100  *    collision probability.
101  *
102  *  - Symbols in different crates but with same names "within" the crate need
103  *    to get different linkage-names.
104  *
105  *  - The hash shown in the filename needs to be predictable and stable for
106  *    build tooling integration. It also needs to be using a hash function
107  *    which is easy to use from Python, make, etc.
108  *
109  * So here is what we do:
110  *
111  *  - Consider the package id; every crate has one (specified with crate_id
112  *    attribute).  If a package id isn't provided explicitly, we infer a
113  *    versionless one from the output name. The version will end up being 0.0
114  *    in this case. CNAME and CVERS are taken from this package id. For
115  *    example, github.com/mozilla/CNAME#CVERS.
116  *
117  *  - Define CMH as SHA256(crateid).
118  *
119  *  - Define CMH8 as the first 8 characters of CMH.
120  *
121  *  - Compile our crate to lib CNAME-CMH8-CVERS.so
122  *
123  *  - Define STH(sym) as SHA256(CMH, type_str(sym))
124  *
125  *  - Suffix a mangled sym with ::STH@CVERS, so that it is unique in the
126  *    name, non-name metadata, and type sense, and versioned in the way
127  *    system linkers understand.
128  */
129
130 pub fn find_crate_name(sess: Option<&Session>,
131                        attrs: &[ast::Attribute],
132                        input: &Input) -> String {
133     let validate = |s: String, span: Option<Span>| {
134         creader::validate_crate_name(sess, &s[..], span);
135         s
136     };
137
138     // Look in attributes 100% of the time to make sure the attribute is marked
139     // as used. After doing this, however, we still prioritize a crate name from
140     // the command line over one found in the #[crate_name] attribute. If we
141     // find both we ensure that they're the same later on as well.
142     let attr_crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
143                                .and_then(|at| at.value_str().map(|s| (at, s)));
144
145     if let Some(sess) = sess {
146         if let Some(ref s) = sess.opts.crate_name {
147             if let Some((attr, ref name)) = attr_crate_name {
148                 if *s != &name[..] {
149                     let msg = format!("--crate-name and #[crate_name] are \
150                                        required to match, but `{}` != `{}`",
151                                       s, name);
152                     sess.span_err(attr.span, &msg[..]);
153                 }
154             }
155             return validate(s.clone(), None);
156         }
157     }
158
159     if let Some((attr, s)) = attr_crate_name {
160         return validate(s.to_string(), Some(attr.span));
161     }
162     if let Input::File(ref path) = *input {
163         if let Some(s) = path.file_stem().and_then(|s| s.to_str()) {
164             if s.starts_with("-") {
165                 let msg = format!("crate names cannot start with a `-`, but \
166                                    `{}` has a leading hyphen", s);
167                 if let Some(sess) = sess {
168                     sess.err(&msg);
169                 }
170             } else {
171                 return validate(s.replace("-", "_"), None);
172             }
173         }
174     }
175
176     "rust_out".to_string()
177 }
178
179 pub fn build_link_meta(sess: &Session, krate: &ast::Crate,
180                        name: String) -> LinkMeta {
181     let r = LinkMeta {
182         crate_name: name,
183         crate_hash: Svh::calculate(&sess.opts.cg.metadata, krate),
184     };
185     info!("{:?}", r);
186     return r;
187 }
188
189 fn truncated_hash_result(symbol_hasher: &mut Sha256) -> String {
190     let output = symbol_hasher.result_bytes();
191     // 64 bits should be enough to avoid collisions.
192     output[.. 8].to_hex().to_string()
193 }
194
195
196 // This calculates STH for a symbol, as defined above
197 fn symbol_hash<'tcx>(tcx: &ty::ctxt<'tcx>,
198                      symbol_hasher: &mut Sha256,
199                      t: Ty<'tcx>,
200                      link_meta: &LinkMeta)
201                      -> String {
202     // NB: do *not* use abbrevs here as we want the symbol names
203     // to be independent of one another in the crate.
204
205     symbol_hasher.reset();
206     symbol_hasher.input_str(&link_meta.crate_name);
207     symbol_hasher.input_str("-");
208     symbol_hasher.input_str(link_meta.crate_hash.as_str());
209     for meta in tcx.sess.crate_metadata.borrow().iter() {
210         symbol_hasher.input_str(&meta[..]);
211     }
212     symbol_hasher.input_str("-");
213     symbol_hasher.input_str(&encoder::encoded_ty(tcx, t));
214     // Prefix with 'h' so that it never blends into adjacent digits
215     let mut hash = String::from("h");
216     hash.push_str(&truncated_hash_result(symbol_hasher));
217     hash
218 }
219
220 fn get_symbol_hash<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> String {
221     match ccx.type_hashcodes().borrow().get(&t) {
222         Some(h) => return h.to_string(),
223         None => {}
224     }
225
226     let mut symbol_hasher = ccx.symbol_hasher().borrow_mut();
227     let hash = symbol_hash(ccx.tcx(), &mut *symbol_hasher, t, ccx.link_meta());
228     ccx.type_hashcodes().borrow_mut().insert(t, hash.clone());
229     hash
230 }
231
232
233 // Name sanitation. LLVM will happily accept identifiers with weird names, but
234 // gas doesn't!
235 // gas accepts the following characters in symbols: a-z, A-Z, 0-9, ., _, $
236 pub fn sanitize(s: &str) -> String {
237     let mut result = String::new();
238     for c in s.chars() {
239         match c {
240             // Escape these with $ sequences
241             '@' => result.push_str("$SP$"),
242             '*' => result.push_str("$BP$"),
243             '&' => result.push_str("$RF$"),
244             '<' => result.push_str("$LT$"),
245             '>' => result.push_str("$GT$"),
246             '(' => result.push_str("$LP$"),
247             ')' => result.push_str("$RP$"),
248             ',' => result.push_str("$C$"),
249
250             // '.' doesn't occur in types and functions, so reuse it
251             // for ':' and '-'
252             '-' | ':' => result.push('.'),
253
254             // These are legal symbols
255             'a' ... 'z'
256             | 'A' ... 'Z'
257             | '0' ... '9'
258             | '_' | '.' | '$' => result.push(c),
259
260             _ => {
261                 result.push('$');
262                 for c in c.escape_unicode().skip(1) {
263                     match c {
264                         '{' => {},
265                         '}' => result.push('$'),
266                         c => result.push(c),
267                     }
268                 }
269             }
270         }
271     }
272
273     // Underscore-qualify anything that didn't start as an ident.
274     if !result.is_empty() &&
275         result.as_bytes()[0] != '_' as u8 &&
276         ! (result.as_bytes()[0] as char).is_xid_start() {
277         return format!("_{}", &result[..]);
278     }
279
280     return result;
281 }
282
283 pub fn mangle<PI: Iterator<Item=PathElem>>(path: PI,
284                                            hash: Option<&str>) -> String {
285     // Follow C++ namespace-mangling style, see
286     // http://en.wikipedia.org/wiki/Name_mangling for more info.
287     //
288     // It turns out that on OSX you can actually have arbitrary symbols in
289     // function names (at least when given to LLVM), but this is not possible
290     // when using unix's linker. Perhaps one day when we just use a linker from LLVM
291     // we won't need to do this name mangling. The problem with name mangling is
292     // that it seriously limits the available characters. For example we can't
293     // have things like &T in symbol names when one would theoretically
294     // want them for things like impls of traits on that type.
295     //
296     // To be able to work on all platforms and get *some* reasonable output, we
297     // use C++ name-mangling.
298
299     let mut n = String::from("_ZN"); // _Z == Begin name-sequence, N == nested
300
301     fn push(n: &mut String, s: &str) {
302         let sani = sanitize(s);
303         n.push_str(&format!("{}{}", sani.len(), sani));
304     }
305
306     // First, connect each component with <len, name> pairs.
307     for e in path {
308         push(&mut n, &token::get_name(e.name()))
309     }
310
311     match hash {
312         Some(s) => push(&mut n, s),
313         None => {}
314     }
315
316     n.push('E'); // End name-sequence.
317     n
318 }
319
320 pub fn exported_name(path: PathElems, hash: &str) -> String {
321     mangle(path, Some(hash))
322 }
323
324 pub fn mangle_exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, path: PathElems,
325                                       t: Ty<'tcx>, id: ast::NodeId) -> String {
326     let mut hash = get_symbol_hash(ccx, t);
327
328     // Paths can be completely identical for different nodes,
329     // e.g. `fn foo() { { fn a() {} } { fn a() {} } }`, so we
330     // generate unique characters from the node id. For now
331     // hopefully 3 characters is enough to avoid collisions.
332     const EXTRA_CHARS: &'static str =
333         "abcdefghijklmnopqrstuvwxyz\
334          ABCDEFGHIJKLMNOPQRSTUVWXYZ\
335          0123456789";
336     let id = id as usize;
337     let extra1 = id % EXTRA_CHARS.len();
338     let id = id / EXTRA_CHARS.len();
339     let extra2 = id % EXTRA_CHARS.len();
340     let id = id / EXTRA_CHARS.len();
341     let extra3 = id % EXTRA_CHARS.len();
342     hash.push(EXTRA_CHARS.as_bytes()[extra1] as char);
343     hash.push(EXTRA_CHARS.as_bytes()[extra2] as char);
344     hash.push(EXTRA_CHARS.as_bytes()[extra3] as char);
345
346     exported_name(path, &hash[..])
347 }
348
349 pub fn mangle_internal_name_by_type_and_seq<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
350                                                       t: Ty<'tcx>,
351                                                       name: &str) -> String {
352     let path = [PathName(token::intern(&t.to_string())),
353                 gensym_name(name)];
354     let hash = get_symbol_hash(ccx, t);
355     mangle(path.iter().cloned(), Some(&hash[..]))
356 }
357
358 pub fn mangle_internal_name_by_path_and_seq(path: PathElems, flav: &str) -> String {
359     mangle(path.chain(Some(gensym_name(flav))), None)
360 }
361
362 pub fn get_linker(sess: &Session) -> (String, Command) {
363     if let Some(ref linker) = sess.opts.cg.linker {
364         (linker.clone(), Command::new(linker))
365     } else if sess.target.target.options.is_like_msvc {
366         ("link.exe".to_string(), msvc::link_exe_cmd(sess))
367     } else {
368         (sess.target.target.options.linker.clone(),
369          Command::new(&sess.target.target.options.linker))
370     }
371 }
372
373 pub fn get_ar_prog(sess: &Session) -> String {
374     sess.opts.cg.ar.clone().unwrap_or_else(|| {
375         sess.target.target.options.ar.clone()
376     })
377 }
378
379 fn command_path(sess: &Session) -> OsString {
380     // The compiler's sysroot often has some bundled tools, so add it to the
381     // PATH for the child.
382     let mut new_path = sess.host_filesearch(PathKind::All)
383                            .get_tools_search_paths();
384     if let Some(path) = env::var_os("PATH") {
385         new_path.extend(env::split_paths(&path));
386     }
387     env::join_paths(new_path).unwrap()
388 }
389
390 pub fn remove(sess: &Session, path: &Path) {
391     match fs::remove_file(path) {
392         Ok(..) => {}
393         Err(e) => {
394             sess.err(&format!("failed to remove {}: {}",
395                              path.display(),
396                              e));
397         }
398     }
399 }
400
401 /// Perform the linkage portion of the compilation phase. This will generate all
402 /// of the requested outputs for this compilation session.
403 pub fn link_binary(sess: &Session,
404                    trans: &CrateTranslation,
405                    outputs: &OutputFilenames,
406                    crate_name: &str) -> Vec<PathBuf> {
407     let mut out_filenames = Vec::new();
408     for &crate_type in sess.crate_types.borrow().iter() {
409         if invalid_output_for_target(sess, crate_type) {
410             sess.bug(&format!("invalid output type `{:?}` for target os `{}`",
411                              crate_type, sess.opts.target_triple));
412         }
413         let out_file = link_binary_output(sess, trans, crate_type, outputs,
414                                           crate_name);
415         out_filenames.push(out_file);
416     }
417
418     // Remove the temporary object file and metadata if we aren't saving temps
419     if !sess.opts.cg.save_temps {
420         for obj in object_filenames(sess, outputs) {
421             remove(sess, &obj);
422         }
423         remove(sess, &outputs.with_extension("metadata.o"));
424     }
425
426     out_filenames
427 }
428
429
430 /// Returns default crate type for target
431 ///
432 /// Default crate type is used when crate type isn't provided neither
433 /// through cmd line arguments nor through crate attributes
434 ///
435 /// It is CrateTypeExecutable for all platforms but iOS as there is no
436 /// way to run iOS binaries anyway without jailbreaking and
437 /// interaction with Rust code through static library is the only
438 /// option for now
439 pub fn default_output_for_target(sess: &Session) -> config::CrateType {
440     if !sess.target.target.options.executables {
441         config::CrateTypeStaticlib
442     } else {
443         config::CrateTypeExecutable
444     }
445 }
446
447 /// Checks if target supports crate_type as output
448 pub fn invalid_output_for_target(sess: &Session,
449                                  crate_type: config::CrateType) -> bool {
450     match (sess.target.target.options.dynamic_linking,
451            sess.target.target.options.executables, crate_type) {
452         (false, _, config::CrateTypeDylib) => true,
453         (_, false, config::CrateTypeExecutable) => true,
454         _ => false
455     }
456 }
457
458 fn is_writeable(p: &Path) -> bool {
459     match p.metadata() {
460         Err(..) => true,
461         Ok(m) => !m.permissions().readonly()
462     }
463 }
464
465 pub fn filename_for_input(sess: &Session,
466                           crate_type: config::CrateType,
467                           crate_name: &str,
468                           outputs: &OutputFilenames) -> PathBuf {
469     let libname = format!("{}{}", crate_name, sess.opts.cg.extra_filename);
470     match crate_type {
471         config::CrateTypeRlib => {
472             outputs.out_directory.join(&format!("lib{}.rlib", libname))
473         }
474         config::CrateTypeDylib => {
475             let (prefix, suffix) = (&sess.target.target.options.dll_prefix,
476                                     &sess.target.target.options.dll_suffix);
477             outputs.out_directory.join(&format!("{}{}{}", prefix, libname,
478                                                 suffix))
479         }
480         config::CrateTypeStaticlib => {
481             outputs.out_directory.join(&format!("lib{}.a", libname))
482         }
483         config::CrateTypeExecutable => {
484             let suffix = &sess.target.target.options.exe_suffix;
485             let out_filename = outputs.path(OutputTypeExe);
486             if suffix.is_empty() {
487                 out_filename.to_path_buf()
488             } else {
489                 out_filename.with_extension(&suffix[1..])
490             }
491         }
492     }
493 }
494
495 fn link_binary_output(sess: &Session,
496                       trans: &CrateTranslation,
497                       crate_type: config::CrateType,
498                       outputs: &OutputFilenames,
499                       crate_name: &str) -> PathBuf {
500     let objects = object_filenames(sess, outputs);
501     let out_filename = match outputs.single_output_file {
502         Some(ref file) => file.clone(),
503         None => filename_for_input(sess, crate_type, crate_name, outputs),
504     };
505
506     // Make sure files are writeable.  Mac, FreeBSD, and Windows system linkers
507     // check this already -- however, the Linux linker will happily overwrite a
508     // read-only file.  We should be consistent.
509     for file in objects.iter().chain(Some(&out_filename)) {
510         if !is_writeable(file) {
511             sess.fatal(&format!("output file {} is not writeable -- check its \
512                                 permissions", file.display()));
513         }
514     }
515
516     match crate_type {
517         config::CrateTypeRlib => {
518             link_rlib(sess, Some(trans), &objects, &out_filename).build();
519         }
520         config::CrateTypeStaticlib => {
521             link_staticlib(sess, &objects, &out_filename);
522         }
523         config::CrateTypeExecutable => {
524             link_natively(sess, trans, false, &objects, &out_filename, outputs);
525         }
526         config::CrateTypeDylib => {
527             link_natively(sess, trans, true, &objects, &out_filename, outputs);
528         }
529     }
530
531     out_filename
532 }
533
534 fn object_filenames(sess: &Session, outputs: &OutputFilenames) -> Vec<PathBuf> {
535     (0..sess.opts.cg.codegen_units).map(|i| {
536         let ext = format!("{}.o", i);
537         outputs.temp_path(OutputTypeObject).with_extension(&ext)
538     }).collect()
539 }
540
541 fn archive_search_paths(sess: &Session) -> Vec<PathBuf> {
542     let mut search = Vec::new();
543     sess.target_filesearch(PathKind::Native).for_each_lib_search_path(|path, _| {
544         search.push(path.to_path_buf());
545         FileDoesntMatch
546     });
547     return search;
548 }
549
550 fn archive_config<'a>(sess: &'a Session,
551                       output: &Path) -> ArchiveConfig<'a> {
552     ArchiveConfig {
553         handler: &sess.diagnostic().handler,
554         dst: output.to_path_buf(),
555         lib_search_paths: archive_search_paths(sess),
556         slib_prefix: sess.target.target.options.staticlib_prefix.clone(),
557         slib_suffix: sess.target.target.options.staticlib_suffix.clone(),
558         ar_prog: get_ar_prog(sess),
559         command_path: command_path(sess),
560     }
561 }
562
563 // Create an 'rlib'
564 //
565 // An rlib in its current incarnation is essentially a renamed .a file. The
566 // rlib primarily contains the object file of the crate, but it also contains
567 // all of the object files from native libraries. This is done by unzipping
568 // native libraries and inserting all of the contents into this archive.
569 fn link_rlib<'a>(sess: &'a Session,
570                  trans: Option<&CrateTranslation>, // None == no metadata/bytecode
571                  objects: &[PathBuf],
572                  out_filename: &Path) -> ArchiveBuilder<'a> {
573     info!("preparing rlib from {:?} to {:?}", objects, out_filename);
574     let mut ab = ArchiveBuilder::create(archive_config(sess, out_filename));
575     for obj in objects {
576         ab.add_file(obj).unwrap();
577     }
578
579     for &(ref l, kind) in sess.cstore.get_used_libraries().borrow().iter() {
580         match kind {
581             cstore::NativeStatic => ab.add_native_library(&l).unwrap(),
582             cstore::NativeFramework | cstore::NativeUnknown => {}
583         }
584     }
585
586     // After adding all files to the archive, we need to update the
587     // symbol table of the archive.
588     ab.update_symbols();
589
590     let mut ab = match sess.target.target.options.is_like_osx {
591         // For OSX/iOS, we must be careful to update symbols only when adding
592         // object files.  We're about to start adding non-object files, so run
593         // `ar` now to process the object files.
594         true => ab.build().extend(),
595         false => ab,
596     };
597
598     // Note that it is important that we add all of our non-object "magical
599     // files" *after* all of the object files in the archive. The reason for
600     // this is as follows:
601     //
602     // * When performing LTO, this archive will be modified to remove
603     //   objects from above. The reason for this is described below.
604     //
605     // * When the system linker looks at an archive, it will attempt to
606     //   determine the architecture of the archive in order to see whether its
607     //   linkable.
608     //
609     //   The algorithm for this detection is: iterate over the files in the
610     //   archive. Skip magical SYMDEF names. Interpret the first file as an
611     //   object file. Read architecture from the object file.
612     //
613     // * As one can probably see, if "metadata" and "foo.bc" were placed
614     //   before all of the objects, then the architecture of this archive would
615     //   not be correctly inferred once 'foo.o' is removed.
616     //
617     // Basically, all this means is that this code should not move above the
618     // code above.
619     match trans {
620         Some(trans) => {
621             // Instead of putting the metadata in an object file section, rlibs
622             // contain the metadata in a separate file. We use a temp directory
623             // here so concurrent builds in the same directory don't try to use
624             // the same filename for metadata (stomping over one another)
625             let tmpdir = TempDir::new("rustc").ok().expect("needs a temp dir");
626             let metadata = tmpdir.path().join(METADATA_FILENAME);
627             match fs::File::create(&metadata).and_then(|mut f| {
628                 f.write_all(&trans.metadata)
629             }) {
630                 Ok(..) => {}
631                 Err(e) => {
632                     sess.fatal(&format!("failed to write {}: {}",
633                                         metadata.display(), e));
634                 }
635             }
636             ab.add_file(&metadata).unwrap();
637             remove(sess, &metadata);
638
639             // For LTO purposes, the bytecode of this library is also inserted
640             // into the archive.  If codegen_units > 1, we insert each of the
641             // bitcode files.
642             for obj in objects {
643                 // Note that we make sure that the bytecode filename in the
644                 // archive is never exactly 16 bytes long by adding a 16 byte
645                 // extension to it. This is to work around a bug in LLDB that
646                 // would cause it to crash if the name of a file in an archive
647                 // was exactly 16 bytes.
648                 let bc_filename = obj.with_extension("bc");
649                 let bc_deflated_filename = obj.with_extension("bytecode.deflate");
650
651                 let mut bc_data = Vec::new();
652                 match fs::File::open(&bc_filename).and_then(|mut f| {
653                     f.read_to_end(&mut bc_data)
654                 }) {
655                     Ok(..) => {}
656                     Err(e) => sess.fatal(&format!("failed to read bytecode: {}",
657                                                  e))
658                 }
659
660                 let bc_data_deflated = flate::deflate_bytes(&bc_data[..]);
661
662                 let mut bc_file_deflated = match fs::File::create(&bc_deflated_filename) {
663                     Ok(file) => file,
664                     Err(e) => {
665                         sess.fatal(&format!("failed to create compressed \
666                                              bytecode file: {}", e))
667                     }
668                 };
669
670                 match write_rlib_bytecode_object_v1(&mut bc_file_deflated,
671                                                     &bc_data_deflated) {
672                     Ok(()) => {}
673                     Err(e) => {
674                         sess.fatal(&format!("failed to write compressed \
675                                              bytecode: {}", e));
676                     }
677                 };
678
679                 ab.add_file(&bc_deflated_filename).unwrap();
680                 remove(sess, &bc_deflated_filename);
681
682                 // See the bottom of back::write::run_passes for an explanation
683                 // of when we do and don't keep .0.bc files around.
684                 let user_wants_numbered_bitcode =
685                         sess.opts.output_types.contains(&OutputTypeBitcode) &&
686                         sess.opts.cg.codegen_units > 1;
687                 if !sess.opts.cg.save_temps && !user_wants_numbered_bitcode {
688                     remove(sess, &bc_filename);
689                 }
690             }
691
692             // After adding all files to the archive, we need to update the
693             // symbol table of the archive. This currently dies on OSX (see
694             // #11162), and isn't necessary there anyway
695             if !sess.target.target.options.is_like_osx {
696                 ab.update_symbols();
697             }
698         }
699
700         None => {}
701     }
702
703     ab
704 }
705
706 fn write_rlib_bytecode_object_v1(writer: &mut Write,
707                                  bc_data_deflated: &[u8]) -> io::Result<()> {
708     let bc_data_deflated_size: u64 = bc_data_deflated.len() as u64;
709
710     try!(writer.write_all(RLIB_BYTECODE_OBJECT_MAGIC));
711     try!(writer.write_all(&[1, 0, 0, 0]));
712     try!(writer.write_all(&[
713         (bc_data_deflated_size >>  0) as u8,
714         (bc_data_deflated_size >>  8) as u8,
715         (bc_data_deflated_size >> 16) as u8,
716         (bc_data_deflated_size >> 24) as u8,
717         (bc_data_deflated_size >> 32) as u8,
718         (bc_data_deflated_size >> 40) as u8,
719         (bc_data_deflated_size >> 48) as u8,
720         (bc_data_deflated_size >> 56) as u8,
721     ]));
722     try!(writer.write_all(&bc_data_deflated));
723
724     let number_of_bytes_written_so_far =
725         RLIB_BYTECODE_OBJECT_MAGIC.len() +                // magic id
726         mem::size_of_val(&RLIB_BYTECODE_OBJECT_VERSION) + // version
727         mem::size_of_val(&bc_data_deflated_size) +        // data size field
728         bc_data_deflated_size as usize;                    // actual data
729
730     // If the number of bytes written to the object so far is odd, add a
731     // padding byte to make it even. This works around a crash bug in LLDB
732     // (see issue #15950)
733     if number_of_bytes_written_so_far % 2 == 1 {
734         try!(writer.write_all(&[0]));
735     }
736
737     return Ok(());
738 }
739
740 // Create a static archive
741 //
742 // This is essentially the same thing as an rlib, but it also involves adding
743 // all of the upstream crates' objects into the archive. This will slurp in
744 // all of the native libraries of upstream dependencies as well.
745 //
746 // Additionally, there's no way for us to link dynamic libraries, so we warn
747 // about all dynamic library dependencies that they're not linked in.
748 //
749 // There's no need to include metadata in a static archive, so ensure to not
750 // link in the metadata object file (and also don't prepare the archive with a
751 // metadata file).
752 fn link_staticlib(sess: &Session, objects: &[PathBuf], out_filename: &Path) {
753     let ab = link_rlib(sess, None, objects, out_filename);
754     let mut ab = match sess.target.target.options.is_like_osx {
755         true => ab.build().extend(),
756         false => ab,
757     };
758     if sess.target.target.options.morestack {
759         ab.add_native_library("morestack").unwrap();
760     }
761     if !sess.target.target.options.no_compiler_rt {
762         ab.add_native_library("compiler-rt").unwrap();
763     }
764
765     let crates = sess.cstore.get_used_crates(cstore::RequireStatic);
766     let mut all_native_libs = vec![];
767
768     for &(cnum, ref path) in &crates {
769         let ref name = sess.cstore.get_crate_data(cnum).name;
770         let p = match *path {
771             Some(ref p) => p.clone(), None => {
772                 sess.err(&format!("could not find rlib for: `{}`",
773                                  name));
774                 continue
775             }
776         };
777         ab.add_rlib(&p, &name[..], sess.lto()).unwrap();
778
779         let native_libs = csearch::get_native_libraries(&sess.cstore, cnum);
780         all_native_libs.extend(native_libs);
781     }
782
783     ab.update_symbols();
784     let _ = ab.build();
785
786     if !all_native_libs.is_empty() {
787         sess.note("link against the following native artifacts when linking against \
788                   this static library");
789         sess.note("the order and any duplication can be significant on some platforms, \
790                   and so may need to be preserved");
791     }
792
793     for &(kind, ref lib) in &all_native_libs {
794         let name = match kind {
795             cstore::NativeStatic => "static library",
796             cstore::NativeUnknown => "library",
797             cstore::NativeFramework => "framework",
798         };
799         sess.note(&format!("{}: {}", name, *lib));
800     }
801 }
802
803 // Create a dynamic library or executable
804 //
805 // This will invoke the system linker/cc to create the resulting file. This
806 // links to all upstream files as well.
807 fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool,
808                  objects: &[PathBuf], out_filename: &Path,
809                  outputs: &OutputFilenames) {
810     info!("preparing dylib? ({}) from {:?} to {:?}", dylib, objects,
811           out_filename);
812     let tmpdir = TempDir::new("rustc").ok().expect("needs a temp dir");
813
814     // The invocations of cc share some flags across platforms
815     let (pname, mut cmd) = get_linker(sess);
816     cmd.env("PATH", command_path(sess));
817
818     let root = sess.target_filesearch(PathKind::Native).get_lib_path();
819     cmd.args(&sess.target.target.options.pre_link_args);
820     for obj in &sess.target.target.options.pre_link_objects {
821         cmd.arg(root.join(obj));
822     }
823
824     {
825         let mut linker = if sess.target.target.options.is_like_msvc {
826             Box::new(MsvcLinker { cmd: &mut cmd, sess: &sess }) as Box<Linker>
827         } else {
828             Box::new(GnuLinker { cmd: &mut cmd, sess: &sess }) as Box<Linker>
829         };
830         link_args(&mut *linker, sess, dylib, tmpdir.path(),
831                   trans, objects, out_filename, outputs);
832         if !sess.target.target.options.no_compiler_rt {
833             linker.link_staticlib("compiler-rt");
834         }
835     }
836     for obj in &sess.target.target.options.post_link_objects {
837         cmd.arg(root.join(obj));
838     }
839     cmd.args(&sess.target.target.options.post_link_args);
840
841     if sess.opts.debugging_opts.print_link_args {
842         println!("{:?}", &cmd);
843     }
844
845     // May have not found libraries in the right formats.
846     sess.abort_if_errors();
847
848     // Invoke the system linker
849     info!("{:?}", &cmd);
850     let prog = time(sess.time_passes(), "running linker", (), |()| cmd.output());
851     match prog {
852         Ok(prog) => {
853             if !prog.status.success() {
854                 sess.err(&format!("linking with `{}` failed: {}",
855                                  pname,
856                                  prog.status));
857                 sess.note(&format!("{:?}", &cmd));
858                 let mut output = prog.stderr.clone();
859                 output.push_all(&prog.stdout);
860                 sess.note(str::from_utf8(&output[..]).unwrap());
861                 sess.abort_if_errors();
862             }
863             info!("linker stderr:\n{}", String::from_utf8(prog.stderr).unwrap());
864             info!("linker stdout:\n{}", String::from_utf8(prog.stdout).unwrap());
865         },
866         Err(e) => {
867             sess.fatal(&format!("could not exec the linker `{}`: {}", pname, e));
868         }
869     }
870
871
872     // On OSX, debuggers need this utility to get run to do some munging of
873     // the symbols
874     if sess.target.target.options.is_like_osx && sess.opts.debuginfo != NoDebugInfo {
875         match Command::new("dsymutil").arg(out_filename).output() {
876             Ok(..) => {}
877             Err(e) => sess.fatal(&format!("failed to run dsymutil: {}", e)),
878         }
879     }
880 }
881
882 fn link_args(cmd: &mut Linker,
883              sess: &Session,
884              dylib: bool,
885              tmpdir: &Path,
886              trans: &CrateTranslation,
887              objects: &[PathBuf],
888              out_filename: &Path,
889              outputs: &OutputFilenames) {
890
891     // The default library location, we need this to find the runtime.
892     // The location of crates will be determined as needed.
893     let lib_path = sess.target_filesearch(PathKind::All).get_lib_path();
894
895     // target descriptor
896     let t = &sess.target.target;
897
898     cmd.include_path(&fix_windows_verbatim_for_gcc(&lib_path));
899     for obj in objects {
900         cmd.add_object(obj);
901     }
902     cmd.output_filename(out_filename);
903
904     // Stack growth requires statically linking a __morestack function. Note
905     // that this is listed *before* all other libraries. Due to the usage of the
906     // --as-needed flag below, the standard library may only be useful for its
907     // rust_stack_exhausted function. In this case, we must ensure that the
908     // libmorestack.a file appears *before* the standard library (so we put it
909     // at the very front).
910     //
911     // Most of the time this is sufficient, except for when LLVM gets super
912     // clever. If, for example, we have a main function `fn main() {}`, LLVM
913     // will optimize out calls to `__morestack` entirely because the function
914     // doesn't need any stack at all!
915     //
916     // To get around this snag, we specially tell the linker to always include
917     // all contents of this library. This way we're guaranteed that the linker
918     // will include the __morestack symbol 100% of the time, always resolving
919     // references to it even if the object above didn't use it.
920     if t.options.morestack {
921         cmd.link_whole_staticlib("morestack", &[lib_path]);
922     }
923
924     // When linking a dynamic library, we put the metadata into a section of the
925     // executable. This metadata is in a separate object file from the main
926     // object file, so we link that in here.
927     if dylib {
928         cmd.add_object(&outputs.with_extension("metadata.o"));
929     }
930
931     // Try to strip as much out of the generated object by removing unused
932     // sections if possible. See more comments in linker.rs
933     cmd.gc_sections(dylib);
934
935     let used_link_args = sess.cstore.get_used_link_args().borrow();
936
937     if !dylib && t.options.position_independent_executables {
938         let empty_vec = Vec::new();
939         let empty_str = String::new();
940         let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec);
941         let mut args = args.iter().chain(used_link_args.iter());
942         let relocation_model = sess.opts.cg.relocation_model.as_ref()
943                                    .unwrap_or(&empty_str);
944         if (t.options.relocation_model == "pic" || *relocation_model == "pic")
945             && !args.any(|x| *x == "-static") {
946             cmd.position_independent_executable();
947         }
948     }
949
950     // Pass optimization flags down to the linker.
951     cmd.optimize();
952
953     // We want to prevent the compiler from accidentally leaking in any system
954     // libraries, so we explicitly ask gcc to not link to any libraries by
955     // default. Note that this does not happen for windows because windows pulls
956     // in some large number of libraries and I couldn't quite figure out which
957     // subset we wanted.
958     cmd.no_default_libraries();
959
960     // Take careful note of the ordering of the arguments we pass to the linker
961     // here. Linkers will assume that things on the left depend on things to the
962     // right. Things on the right cannot depend on things on the left. This is
963     // all formally implemented in terms of resolving symbols (libs on the right
964     // resolve unknown symbols of libs on the left, but not vice versa).
965     //
966     // For this reason, we have organized the arguments we pass to the linker as
967     // such:
968     //
969     //  1. The local object that LLVM just generated
970     //  2. Upstream rust libraries
971     //  3. Local native libraries
972     //  4. Upstream native libraries
973     //
974     // This is generally fairly natural, but some may expect 2 and 3 to be
975     // swapped. The reason that all native libraries are put last is that it's
976     // not recommended for a native library to depend on a symbol from a rust
977     // crate. If this is the case then a staticlib crate is recommended, solving
978     // the problem.
979     //
980     // Additionally, it is occasionally the case that upstream rust libraries
981     // depend on a local native library. In the case of libraries such as
982     // lua/glfw/etc the name of the library isn't the same across all platforms,
983     // so only the consumer crate of a library knows the actual name. This means
984     // that downstream crates will provide the #[link] attribute which upstream
985     // crates will depend on. Hence local native libraries are after out
986     // upstream rust crates.
987     //
988     // In theory this means that a symbol in an upstream native library will be
989     // shadowed by a local native library when it wouldn't have been before, but
990     // this kind of behavior is pretty platform specific and generally not
991     // recommended anyway, so I don't think we're shooting ourself in the foot
992     // much with that.
993     add_upstream_rust_crates(cmd, sess, dylib, tmpdir, trans);
994     add_local_native_libraries(cmd, sess);
995     add_upstream_native_libraries(cmd, sess);
996
997     // # Telling the linker what we're doing
998
999     if dylib {
1000         cmd.build_dylib(out_filename);
1001     }
1002
1003     // FIXME (#2397): At some point we want to rpath our guesses as to
1004     // where extern libraries might live, based on the
1005     // addl_lib_search_paths
1006     if sess.opts.cg.rpath {
1007         let sysroot = sess.sysroot();
1008         let target_triple = &sess.opts.target_triple;
1009         let mut get_install_prefix_lib_path = || {
1010             let install_prefix = option_env!("CFG_PREFIX").expect("CFG_PREFIX");
1011             let tlib = filesearch::relative_target_lib_path(sysroot, target_triple);
1012             let mut path = PathBuf::from(install_prefix);
1013             path.push(&tlib);
1014
1015             path
1016         };
1017         let mut rpath_config = RPathConfig {
1018             used_crates: sess.cstore.get_used_crates(cstore::RequireDynamic),
1019             out_filename: out_filename.to_path_buf(),
1020             has_rpath: sess.target.target.options.has_rpath,
1021             is_like_osx: sess.target.target.options.is_like_osx,
1022             get_install_prefix_lib_path: &mut get_install_prefix_lib_path,
1023         };
1024         cmd.args(&rpath::get_rpath_flags(&mut rpath_config));
1025     }
1026
1027     // Finally add all the linker arguments provided on the command line along
1028     // with any #[link_args] attributes found inside the crate
1029     if let Some(ref args) = sess.opts.cg.link_args {
1030         cmd.args(args);
1031     }
1032     cmd.args(&used_link_args);
1033 }
1034
1035 // # Native library linking
1036 //
1037 // User-supplied library search paths (-L on the command line). These are
1038 // the same paths used to find Rust crates, so some of them may have been
1039 // added already by the previous crate linking code. This only allows them
1040 // to be found at compile time so it is still entirely up to outside
1041 // forces to make sure that library can be found at runtime.
1042 //
1043 // Also note that the native libraries linked here are only the ones located
1044 // in the current crate. Upstream crates with native library dependencies
1045 // may have their native library pulled in above.
1046 fn add_local_native_libraries(cmd: &mut Linker, sess: &Session) {
1047     sess.target_filesearch(PathKind::All).for_each_lib_search_path(|path, k| {
1048         match k {
1049             PathKind::Framework => { cmd.framework_path(path); }
1050             _ => { cmd.include_path(&fix_windows_verbatim_for_gcc(path)); }
1051         }
1052         FileDoesntMatch
1053     });
1054
1055     let libs = sess.cstore.get_used_libraries();
1056     let libs = libs.borrow();
1057
1058     let staticlibs = libs.iter().filter_map(|&(ref l, kind)| {
1059         if kind == cstore::NativeStatic {Some(l)} else {None}
1060     });
1061     let others = libs.iter().filter(|&&(_, kind)| {
1062         kind != cstore::NativeStatic
1063     });
1064
1065     // Some platforms take hints about whether a library is static or dynamic.
1066     // For those that support this, we ensure we pass the option if the library
1067     // was flagged "static" (most defaults are dynamic) to ensure that if
1068     // libfoo.a and libfoo.so both exist that the right one is chosen.
1069     cmd.hint_static();
1070
1071     let search_path = archive_search_paths(sess);
1072     for l in staticlibs {
1073         // Here we explicitly ask that the entire archive is included into the
1074         // result artifact. For more details see #15460, but the gist is that
1075         // the linker will strip away any unused objects in the archive if we
1076         // don't otherwise explicitly reference them. This can occur for
1077         // libraries which are just providing bindings, libraries with generic
1078         // functions, etc.
1079         cmd.link_whole_staticlib(l, &search_path);
1080     }
1081
1082     cmd.hint_dynamic();
1083
1084     for &(ref l, kind) in others {
1085         match kind {
1086             cstore::NativeUnknown => cmd.link_dylib(l),
1087             cstore::NativeFramework => cmd.link_framework(l),
1088             cstore::NativeStatic => unreachable!(),
1089         }
1090     }
1091 }
1092
1093 // # Rust Crate linking
1094 //
1095 // Rust crates are not considered at all when creating an rlib output. All
1096 // dependencies will be linked when producing the final output (instead of
1097 // the intermediate rlib version)
1098 fn add_upstream_rust_crates(cmd: &mut Linker, sess: &Session,
1099                             dylib: bool, tmpdir: &Path,
1100                             trans: &CrateTranslation) {
1101     // All of the heavy lifting has previously been accomplished by the
1102     // dependency_format module of the compiler. This is just crawling the
1103     // output of that module, adding crates as necessary.
1104     //
1105     // Linking to a rlib involves just passing it to the linker (the linker
1106     // will slurp up the object files inside), and linking to a dynamic library
1107     // involves just passing the right -l flag.
1108
1109     let data = if dylib {
1110         trans.crate_formats.get(&config::CrateTypeDylib).unwrap()
1111     } else {
1112         trans.crate_formats.get(&config::CrateTypeExecutable).unwrap()
1113     };
1114
1115     // Invoke get_used_crates to ensure that we get a topological sorting of
1116     // crates.
1117     let deps = sess.cstore.get_used_crates(cstore::RequireDynamic);
1118
1119     for &(cnum, _) in &deps {
1120         // We may not pass all crates through to the linker. Some crates may
1121         // appear statically in an existing dylib, meaning we'll pick up all the
1122         // symbols from the dylib.
1123         let kind = match data[cnum as usize - 1] {
1124             Some(t) => t,
1125             None => continue
1126         };
1127         let src = sess.cstore.get_used_crate_source(cnum).unwrap();
1128         match kind {
1129             cstore::RequireDynamic => {
1130                 add_dynamic_crate(cmd, sess, &src.dylib.unwrap().0)
1131             }
1132             cstore::RequireStatic => {
1133                 add_static_crate(cmd, sess, tmpdir, dylib, &src.rlib.unwrap().0)
1134             }
1135         }
1136
1137     }
1138
1139     // Converts a library file-stem into a cc -l argument
1140     fn unlib<'a>(config: &config::Config, stem: &'a str) -> &'a str {
1141         if stem.starts_with("lib") && !config.target.options.is_like_windows {
1142             &stem[3..]
1143         } else {
1144             stem
1145         }
1146     }
1147
1148     // Adds the static "rlib" versions of all crates to the command line.
1149     // There's a bit of magic which happens here specifically related to LTO and
1150     // dynamic libraries. Specifically:
1151     //
1152     // * For LTO, we remove upstream object files.
1153     // * For dylibs we remove metadata and bytecode from upstream rlibs
1154     //
1155     // When performing LTO, all of the bytecode from the upstream libraries has
1156     // already been included in our object file output. As a result we need to
1157     // remove the object files in the upstream libraries so the linker doesn't
1158     // try to include them twice (or whine about duplicate symbols). We must
1159     // continue to include the rest of the rlib, however, as it may contain
1160     // static native libraries which must be linked in.
1161     //
1162     // When making a dynamic library, linkers by default don't include any
1163     // object files in an archive if they're not necessary to resolve the link.
1164     // We basically want to convert the archive (rlib) to a dylib, though, so we
1165     // *do* want everything included in the output, regardless of whether the
1166     // linker thinks it's needed or not. As a result we must use the
1167     // --whole-archive option (or the platform equivalent). When using this
1168     // option the linker will fail if there are non-objects in the archive (such
1169     // as our own metadata and/or bytecode). All in all, for rlibs to be
1170     // entirely included in dylibs, we need to remove all non-object files.
1171     //
1172     // Note, however, that if we're not doing LTO or we're not producing a dylib
1173     // (aka we're making an executable), we can just pass the rlib blindly to
1174     // the linker (fast) because it's fine if it's not actually included as
1175     // we're at the end of the dependency chain.
1176     fn add_static_crate(cmd: &mut Linker, sess: &Session, tmpdir: &Path,
1177                         dylib: bool, cratepath: &Path) {
1178         if !sess.lto() && !dylib {
1179             cmd.link_rlib(&fix_windows_verbatim_for_gcc(cratepath));
1180             return
1181         }
1182
1183         let dst = tmpdir.join(cratepath.file_name().unwrap());
1184         let name = cratepath.file_name().unwrap().to_str().unwrap();
1185         let name = &name[3..name.len() - 5]; // chop off lib/.rlib
1186
1187         time(sess.time_passes(), &format!("altering {}.rlib", name), (), |()| {
1188             let err = (|| {
1189                 io::copy(&mut try!(fs::File::open(&cratepath)),
1190                          &mut try!(fs::File::create(&dst)))
1191             })();
1192             if let Err(e) = err {
1193                 sess.fatal(&format!("failed to copy {} to {}: {}",
1194                                     cratepath.display(), dst.display(), e));
1195             }
1196
1197             let mut archive = Archive::open(archive_config(sess, &dst));
1198             archive.remove_file(METADATA_FILENAME);
1199
1200             let mut any_objects = false;
1201             for f in archive.files() {
1202                 if f.ends_with("bytecode.deflate") {
1203                     archive.remove_file(&f);
1204                     continue
1205                 }
1206                 let canonical = f.replace("-", "_");
1207                 let canonical_name = name.replace("-", "_");
1208                 if sess.lto() && canonical.starts_with(&canonical_name) &&
1209                    canonical.ends_with(".o") {
1210                     let num = &f[name.len()..f.len() - 2];
1211                     if num.len() > 0 && num[1..].parse::<u32>().is_ok() {
1212                         archive.remove_file(&f);
1213                         continue
1214                     }
1215                 }
1216                 any_objects = true;
1217             }
1218
1219             if any_objects {
1220                 cmd.link_whole_rlib(&fix_windows_verbatim_for_gcc(&dst));
1221             }
1222         });
1223     }
1224
1225     // Same thing as above, but for dynamic crates instead of static crates.
1226     fn add_dynamic_crate(cmd: &mut Linker, sess: &Session, cratepath: &Path) {
1227         // If we're performing LTO, then it should have been previously required
1228         // that all upstream rust dependencies were available in an rlib format.
1229         assert!(!sess.lto());
1230
1231         // Just need to tell the linker about where the library lives and
1232         // what its name is
1233         let parent = cratepath.parent();
1234         if let Some(dir) = parent {
1235             cmd.include_path(&fix_windows_verbatim_for_gcc(dir));
1236         }
1237         let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
1238         cmd.link_rust_dylib(&unlib(&sess.target, filestem),
1239                             parent.unwrap_or(Path::new("")));
1240     }
1241 }
1242
1243 // Link in all of our upstream crates' native dependencies. Remember that
1244 // all of these upstream native dependencies are all non-static
1245 // dependencies. We've got two cases then:
1246 //
1247 // 1. The upstream crate is an rlib. In this case we *must* link in the
1248 // native dependency because the rlib is just an archive.
1249 //
1250 // 2. The upstream crate is a dylib. In order to use the dylib, we have to
1251 // have the dependency present on the system somewhere. Thus, we don't
1252 // gain a whole lot from not linking in the dynamic dependency to this
1253 // crate as well.
1254 //
1255 // The use case for this is a little subtle. In theory the native
1256 // dependencies of a crate are purely an implementation detail of the crate
1257 // itself, but the problem arises with generic and inlined functions. If a
1258 // generic function calls a native function, then the generic function must
1259 // be instantiated in the target crate, meaning that the native symbol must
1260 // also be resolved in the target crate.
1261 fn add_upstream_native_libraries(cmd: &mut Linker, sess: &Session) {
1262     // Be sure to use a topological sorting of crates because there may be
1263     // interdependencies between native libraries. When passing -nodefaultlibs,
1264     // for example, almost all native libraries depend on libc, so we have to
1265     // make sure that's all the way at the right (liblibc is near the base of
1266     // the dependency chain).
1267     //
1268     // This passes RequireStatic, but the actual requirement doesn't matter,
1269     // we're just getting an ordering of crate numbers, we're not worried about
1270     // the paths.
1271     let crates = sess.cstore.get_used_crates(cstore::RequireStatic);
1272     for (cnum, _) in crates {
1273         let libs = csearch::get_native_libraries(&sess.cstore, cnum);
1274         for &(kind, ref lib) in &libs {
1275             match kind {
1276                 cstore::NativeUnknown => cmd.link_dylib(lib),
1277                 cstore::NativeFramework => cmd.link_framework(lib),
1278                 cstore::NativeStatic => {
1279                     sess.bug("statics shouldn't be propagated");
1280                 }
1281             }
1282         }
1283     }
1284 }