|
| 1 | +// Copyright 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 target::Target; |
| 12 | + |
| 13 | +pub fn target() -> Target { |
| 14 | + let mut base = super::linux_base::opts(); |
| 15 | + base.cpu = "x86-64".to_string(); |
| 16 | + base.linker = "musl-gcc".to_string(); |
| 17 | + base.pre_link_args.push("-m64".to_string()); |
| 18 | + |
| 19 | + // Make sure that the linker/gcc really don't pull in anything, including |
| 20 | + // default objects, libs, etc. |
| 21 | + base.pre_link_args.push("-nostdlib".to_string()); |
| 22 | + base.pre_link_args.push("-static".to_string()); |
| 23 | + |
| 24 | + // At least when this was tested, the linker would not add the |
| 25 | + // `GNU_EH_FRAME` program header to executables generated, which is required |
| 26 | + // when unwinding to locate the unwinding information. I'm not sure why this |
| 27 | + // argument is *not* necessary for normal builds, but it can't hurt! |
| 28 | + base.pre_link_args.push("-Wl,--eh-frame-hdr".to_string()); |
| 29 | + |
| 30 | + // There's a whole bunch of circular dependencies when dealing with MUSL |
| 31 | + // unfortunately. To put this in perspective libc is statically linked to |
| 32 | + // liblibc and libunwind is statically linked to libstd: |
| 33 | + // |
| 34 | + // * libcore depends on `fmod` which is in libc (transitively in liblibc). |
| 35 | + // liblibc, however, depends on libcore. |
| 36 | + // * compiler-rt has personality symbols that depend on libunwind, but |
| 37 | + // libunwind is in libstd which depends on compiler-rt. |
| 38 | + // |
| 39 | + // Recall that linkers discard libraries and object files as much as |
| 40 | + // possible, and with all the static linking and archives flying around with |
| 41 | + // MUSL the linker is super aggressively stripping out objects. For example |
| 42 | + // the first case has fmod stripped from liblibc (it's in its own object |
| 43 | + // file) so it's not there when libcore needs it. In the second example all |
| 44 | + // the unused symbols from libunwind are stripped (each is in its own object |
| 45 | + // file in libstd) before we end up linking compiler-rt which depends on |
| 46 | + // those symbols. |
| 47 | + // |
| 48 | + // To deal with these circular dependencies we just force the compiler to |
| 49 | + // link everything as a group, not stripping anything out until everything |
| 50 | + // is processed. The linker will still perform a pass to strip out object |
| 51 | + // files but it won't do so until all objects/archives have been processed. |
| 52 | + base.pre_link_args.push("-Wl,-(".to_string()); |
| 53 | + base.post_link_args.push("-Wl,-)".to_string()); |
| 54 | + |
| 55 | + // When generating a statically linked executable there's generally some |
| 56 | + // small setup needed which is listed in these files. These are provided by |
| 57 | + // a musl toolchain and are linked by default by the `musl-gcc` script. Note |
| 58 | + // that `gcc` also does this by default, it just uses some different files. |
| 59 | + // |
| 60 | + // Each target directory for musl has these object files included in it so |
| 61 | + // they'll be included from there. |
| 62 | + base.pre_link_objects.push("crt1.o".to_string()); |
| 63 | + base.pre_link_objects.push("crti.o".to_string()); |
| 64 | + base.post_link_objects.push("crtn.o".to_string()); |
| 65 | + |
| 66 | + // MUSL support doesn't currently include dynamic linking, so there's no |
| 67 | + // need for dylibs or rpath business. Additionally `-pie` is incompatible |
| 68 | + // with `-static`, so we can't pass `-pie`. |
| 69 | + base.dynamic_linking = false; |
| 70 | + base.has_rpath = false; |
| 71 | + base.position_independent_executables = false; |
| 72 | + |
| 73 | + Target { |
| 74 | + data_layout: "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-\ |
| 75 | + f32:32:32-f64:64:64-v64:64:64-v128:128:128-a:0:64-\ |
| 76 | + s0:64:64-f80:128:128-n8:16:32:64-S128".to_string(), |
| 77 | + llvm_target: "x86_64-unknown-linux-musl".to_string(), |
| 78 | + target_endian: "little".to_string(), |
| 79 | + target_pointer_width: "64".to_string(), |
| 80 | + arch: "x86_64".to_string(), |
| 81 | + target_os: "linux".to_string(), |
| 82 | + target_env: "musl".to_string(), |
| 83 | + options: base, |
| 84 | + } |
| 85 | +} |
0 commit comments