From 740a31d05df6422ed8e83c80c9740a920403de6b Mon Sep 17 00:00:00 2001 From: JonathanS Date: Wed, 17 Oct 2018 22:13:21 +0200 Subject: [PATCH 01/16] Enable dist-x86_64-musl as a host architexture --- src/ci/docker/dist-x86_64-musl/Dockerfile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ci/docker/dist-x86_64-musl/Dockerfile b/src/ci/docker/dist-x86_64-musl/Dockerfile index 06f8a2fbba836..e6ffac4019946 100644 --- a/src/ci/docker/dist-x86_64-musl/Dockerfile +++ b/src/ci/docker/dist-x86_64-musl/Dockerfile @@ -41,6 +41,8 @@ ENV RUST_CONFIGURE_ARGS \ # See: https://github.com/rust-lang/rust/issues/34978 ENV CFLAGS_x86_64_unknown_linux_musl=-Wa,-mrelax-relocations=no +ENV HOSTS=x86_64-unknown-linux-musl + ENV SCRIPT \ - python2.7 ../x.py test --target x86_64-unknown-linux-musl && \ - python2.7 ../x.py dist --target x86_64-unknown-linux-musl + python2.7 ../x.py test --host $HOSTS --target $HOSTS && \ + python2.7 ../x.py dist --host $HOSTS --target $HOSTS From d97fd93af6de1d723fc802b60e70cd33988a06cb Mon Sep 17 00:00:00 2001 From: JonathanS Date: Wed, 17 Oct 2018 22:14:27 +0200 Subject: [PATCH 02/16] throwaway commit: Test Travis build only on x86_64 musl --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index c4efa88460325..d83778b46ffcc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ matrix: include: # Images used in testing PR and try-build should be run first. - env: IMAGE=x86_64-gnu-llvm-6.0 RUST_BACKTRACE=1 - if: type = pull_request OR branch = auto + if: branch = auto - env: IMAGE=dist-x86_64-linux DEPLOY=1 if: branch = try OR branch = auto @@ -159,7 +159,7 @@ matrix: - env: IMAGE=dist-x86_64-freebsd DEPLOY=1 if: branch = auto - env: IMAGE=dist-x86_64-musl DEPLOY=1 - if: branch = auto +# if: branch = auto - env: IMAGE=dist-x86_64-netbsd DEPLOY=1 if: branch = auto - env: IMAGE=asmjs @@ -185,7 +185,7 @@ matrix: - env: IMAGE=x86_64-gnu-distcheck if: branch = auto - env: IMAGE=mingw-check - if: type = pull_request OR branch = auto + if: branch = auto - stage: publish toolstate if: branch = master AND type = push From ce8e02a3020995fccf20fc54dc07c5d2cd44ac1f Mon Sep 17 00:00:00 2001 From: Jonathan Sieber Date: Thu, 18 Oct 2018 21:47:26 +0200 Subject: [PATCH 03/16] Set RUSTFLAGS env to make dylib work The musl-target doesn't automatically disable static linking of musl when building a dylib, and then complains it can't build a dylib. As a workaround, disable static linking via RUSTFLAGS, to see how far the build gets. The proper fix is to have rustc figure that out automagically. --- src/ci/docker/dist-x86_64-musl/Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ci/docker/dist-x86_64-musl/Dockerfile b/src/ci/docker/dist-x86_64-musl/Dockerfile index e6ffac4019946..441c2e7c57a07 100644 --- a/src/ci/docker/dist-x86_64-musl/Dockerfile +++ b/src/ci/docker/dist-x86_64-musl/Dockerfile @@ -43,6 +43,8 @@ ENV CFLAGS_x86_64_unknown_linux_musl=-Wa,-mrelax-relocations=no ENV HOSTS=x86_64-unknown-linux-musl +ENV RUSTFLAGS="-C target-feature=-crt-static" + ENV SCRIPT \ python2.7 ../x.py test --host $HOSTS --target $HOSTS && \ python2.7 ../x.py dist --host $HOSTS --target $HOSTS From d3d71c16411cfe100ef61dcd4455e4fed5f7abdb Mon Sep 17 00:00:00 2001 From: Jonathan Sieber Date: Fri, 19 Oct 2018 19:04:33 +0000 Subject: [PATCH 04/16] build a proper c++-enabled musl toolchain with musl-cross-make --- src/ci/docker/dist-x86_64-musl/Dockerfile | 22 ++++---- src/ci/docker/scripts/musl-toolchain.sh | 66 +++++++++++++++++++++++ 2 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 src/ci/docker/scripts/musl-toolchain.sh diff --git a/src/ci/docker/dist-x86_64-musl/Dockerfile b/src/ci/docker/dist-x86_64-musl/Dockerfile index 441c2e7c57a07..2f538a28b4364 100644 --- a/src/ci/docker/dist-x86_64-musl/Dockerfile +++ b/src/ci/docker/dist-x86_64-musl/Dockerfile @@ -4,6 +4,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ g++ \ make \ file \ + wget \ curl \ ca-certificates \ python2.7 \ @@ -18,19 +19,17 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ WORKDIR /build/ -COPY scripts/musl.sh /build/ +COPY scripts/musl-toolchain.sh /build/ # We need to mitigate rust-lang/rust#34978 when compiling musl itself as well -RUN CC=gcc \ - CFLAGS="-Wa,-mrelax-relocations=no" \ - CXX=g++ \ - CXXFLAGS="-Wa,-mrelax-relocations=no" \ - bash musl.sh x86_64 && rm -rf /build +# TODO: Check what this issue is and if we can ignore it + +RUN bash musl-toolchain.sh x86_64-linux-musl && rm -rf build COPY scripts/sccache.sh /scripts/ RUN sh /scripts/sccache.sh ENV RUST_CONFIGURE_ARGS \ - --musl-root-x86_64=/musl-x86_64 \ + --musl-root-x86_64=/usr/local/x86_64-linux-musl \ --enable-extended \ --disable-docs @@ -39,9 +38,14 @@ ENV RUST_CONFIGURE_ARGS \ # way to produce "super compatible" binaries. # # See: https://github.com/rust-lang/rust/issues/34978 -ENV CFLAGS_x86_64_unknown_linux_musl=-Wa,-mrelax-relocations=no +#ENV CFLAGS_x86_64_unknown_linux_musl=-Wa,-mrelax-relocations=no + +ENV HOSTS=x86_64-unknown-linux-musl \ + CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc \ + CXX_x86_64_unknown_linux_musl=x86_64-linux-musl-g++ -ENV HOSTS=x86_64-unknown-linux-musl +# CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_LINKER=musl-gcc \ +# CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_RUNNER="qemu-arm -L /musl-arm" ENV RUSTFLAGS="-C target-feature=-crt-static" diff --git a/src/ci/docker/scripts/musl-toolchain.sh b/src/ci/docker/scripts/musl-toolchain.sh new file mode 100644 index 0000000000000..db609a8666f30 --- /dev/null +++ b/src/ci/docker/scripts/musl-toolchain.sh @@ -0,0 +1,66 @@ +# Copyright 2016 The Rust Project Developers. See the COPYRIGHT +# file at the top-level directory of this distribution and at +# http://rust-lang.org/COPYRIGHT. +# +# Licensed under the Apache License, Version 2.0 or the MIT license +# , at your +# option. This file may not be copied, modified, or distributed +# except according to those terms. + +set -ex + +hide_output() { + set +x + on_err=" +echo ERROR: An error was encountered with the build. +cat /tmp/build.log +exit 1 +" + trap "$on_err" ERR + bash -c "while true; do sleep 30; echo \$(date) - building ...; done" & + PING_LOOP_PID=$! + $@ &> /tmp/build.log + trap - ERR + kill $PING_LOOP_PID + rm /tmp/build.log + set -x +} + +TARGET=$1 +OUTPUT=/usr/local +shift + +git clone https://github.com/richfelker/musl-cross-make -b v0.9.7 +cd musl-cross-make + +hide_output make -j$(nproc) TARGET=$TARGET +hide_output make install TARGET=$TARGET OUTPUT=$OUTPUT + +cd .. + +export CC=$TARGET-gcc +export CXX=$TARGET-g++ + +LLVM=60 + +# may have been downloaded in a previous run +if [ ! -d libunwind-release_$LLVM ]; then + curl -L https://github.com/llvm-mirror/llvm/archive/release_$LLVM.tar.gz | tar xzf - + curl -L https://github.com/llvm-mirror/libunwind/archive/release_$LLVM.tar.gz | tar xzf - +fi + +mkdir libunwind-build +cd libunwind-build +cmake ../libunwind-release_$LLVM \ + -DLLVM_PATH=/build/llvm-release_$LLVM \ + -DLIBUNWIND_ENABLE_SHARED=0 \ + -DCMAKE_C_COMPILER=$CC \ + -DCMAKE_CXX_COMPILER=$CXX \ + -DCMAKE_C_FLAGS="$CFLAGS" \ + -DCMAKE_CXX_FLAGS="$CXXFLAGS" + +hide_output make -j$(nproc) +cp lib/libunwind.a $OUTPUT/$TARGET/lib +cd ../ && rm -rf libunwind-build + From 876a39aa0d797889962b9c5b1e56872d90839af6 Mon Sep 17 00:00:00 2001 From: Jonathan Sieber Date: Fri, 19 Oct 2018 20:28:34 +0000 Subject: [PATCH 05/16] Make the musl dynamic loader known to the system, so it can execute target binaries --- src/ci/docker/scripts/musl-toolchain.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/ci/docker/scripts/musl-toolchain.sh b/src/ci/docker/scripts/musl-toolchain.sh index db609a8666f30..0406d5182e8f6 100644 --- a/src/ci/docker/scripts/musl-toolchain.sh +++ b/src/ci/docker/scripts/musl-toolchain.sh @@ -28,6 +28,10 @@ exit 1 } TARGET=$1 +#ARCH=$1 +#TARGET=linux-musl-$ARCH +ARCH=x86_64 + OUTPUT=/usr/local shift @@ -39,6 +43,13 @@ hide_output make install TARGET=$TARGET OUTPUT=$OUTPUT cd .. +# Make musl binaries executable + +ln -s $OUTPUT/$TARGET/lib/ld-musl-$ARCH.so.1 /lib +ln -s $OUTPUT/$TARGET/lib/libc.so /lib +echo $OUTPUT/$TARGET/lib >> /etc/ld-musl-$ARCH.path + + export CC=$TARGET-gcc export CXX=$TARGET-g++ From a193c94122988ed4fd80fe62d186ab3ea685ec84 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Sun, 16 Sep 2018 16:40:04 +0000 Subject: [PATCH 06/16] runtest: Fix proc-macro tests on musl hosts --- src/tools/compiletest/src/runtest.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 400c205d44b20..be5c50b070d5d 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -1566,7 +1566,6 @@ impl<'test> TestCx<'test> { None } else if self.config.target.contains("cloudabi") || self.config.target.contains("emscripten") - || (self.config.target.contains("musl") && !aux_props.force_host) || self.config.target.contains("wasm32") { // We primarily compile all auxiliary libraries as dynamic libraries @@ -1574,10 +1573,8 @@ impl<'test> TestCx<'test> { // for the test suite (otherwise including libstd statically in all // executables takes up quite a bit of space). // - // For targets like MUSL or Emscripten, however, there is no support for - // dynamic libraries so we just go back to building a normal library. Note, - // however, that for MUSL if the library is built with `force_host` then - // it's ok to be a dylib as the host should always support dylibs. + // For targets like Emscripten, however, there is no support for + // dynamic libraries so we just go back to building a normal library. Some("lib") } else { Some("dylib") From 73d682055db22cf6c4d1c3d0cdc2e7c6c79b726e Mon Sep 17 00:00:00 2001 From: Jonathan Sieber Date: Tue, 27 Nov 2018 20:56:34 +0100 Subject: [PATCH 07/16] musl-toolchain: fix global lib paths (dont create /lib/libc.so) --- src/ci/docker/scripts/musl-toolchain.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ci/docker/scripts/musl-toolchain.sh b/src/ci/docker/scripts/musl-toolchain.sh index 0406d5182e8f6..25781c747493a 100644 --- a/src/ci/docker/scripts/musl-toolchain.sh +++ b/src/ci/docker/scripts/musl-toolchain.sh @@ -45,8 +45,7 @@ cd .. # Make musl binaries executable -ln -s $OUTPUT/$TARGET/lib/ld-musl-$ARCH.so.1 /lib -ln -s $OUTPUT/$TARGET/lib/libc.so /lib +ln -s $OUTPUT/$TARGET/lib/libc.so /lib/ld-musl-$ARCH.so.1 echo $OUTPUT/$TARGET/lib >> /etc/ld-musl-$ARCH.path From d68c6263b6855c867d82146aac1b0b39666210cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Thu, 6 Dec 2018 15:50:08 +0100 Subject: [PATCH 08/16] musl: enable dynamic linking in the spec --- src/librustc_target/spec/linux_musl_base.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/librustc_target/spec/linux_musl_base.rs b/src/librustc_target/spec/linux_musl_base.rs index 1bc90d1a73287..d180d4226d78a 100644 --- a/src/librustc_target/spec/linux_musl_base.rs +++ b/src/librustc_target/spec/linux_musl_base.rs @@ -30,5 +30,13 @@ pub fn opts() -> TargetOptions { // These targets allow the user to choose between static and dynamic linking. base.crt_static_respected = true; + // Defaults for dynamic linking + base.dynamic_linking = true; + base.executables = true; + base.has_elf_tls = true; + base.has_rpath = true; + base.position_independent_executables = true; + base.relro_level = true; + base } From fecbc01a3e14b5b3fc448cf68781e3f2b41d2de3 Mon Sep 17 00:00:00 2001 From: Jonathan Sieber Date: Thu, 6 Dec 2018 20:45:33 +0100 Subject: [PATCH 09/16] fix RelroLevel --- src/librustc_target/spec/linux_musl_base.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_target/spec/linux_musl_base.rs b/src/librustc_target/spec/linux_musl_base.rs index d180d4226d78a..bcb242bdf4f39 100644 --- a/src/librustc_target/spec/linux_musl_base.rs +++ b/src/librustc_target/spec/linux_musl_base.rs @@ -1,4 +1,4 @@ -use spec::{LinkerFlavor, TargetOptions}; +use spec::{LinkerFlavor, TargetOptions, RelroLevel}; pub fn opts() -> TargetOptions { let mut base = super::linux_base::opts(); @@ -36,7 +36,7 @@ pub fn opts() -> TargetOptions { base.has_elf_tls = true; base.has_rpath = true; base.position_independent_executables = true; - base.relro_level = true; + base.relro_level = RelroLevel::Full; base } From dc0594ea7056effa53efa6db70c687cd5afaed80 Mon Sep 17 00:00:00 2001 From: Jonathan Sieber Date: Fri, 7 Dec 2018 01:35:18 +0100 Subject: [PATCH 10/16] Revert "Set RUSTFLAGS env to make dylib work" This reverts commit e52ea151b4cff9ea69512b4c46842a95c4ed56d6. --- src/ci/docker/dist-x86_64-musl/Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ci/docker/dist-x86_64-musl/Dockerfile b/src/ci/docker/dist-x86_64-musl/Dockerfile index 2f538a28b4364..dae2b4d10a2e8 100644 --- a/src/ci/docker/dist-x86_64-musl/Dockerfile +++ b/src/ci/docker/dist-x86_64-musl/Dockerfile @@ -47,8 +47,6 @@ ENV HOSTS=x86_64-unknown-linux-musl \ # CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_LINKER=musl-gcc \ # CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_RUNNER="qemu-arm -L /musl-arm" -ENV RUSTFLAGS="-C target-feature=-crt-static" - ENV SCRIPT \ python2.7 ../x.py test --host $HOSTS --target $HOSTS && \ python2.7 ../x.py dist --host $HOSTS --target $HOSTS From 09860473392798a1f2c17a574caddd16658b8e75 Mon Sep 17 00:00:00 2001 From: Martell Malone Date: Sat, 5 Jan 2019 12:59:46 -0800 Subject: [PATCH 11/16] Address review comments --- src/ci/docker/scripts/musl-toolchain.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ci/docker/scripts/musl-toolchain.sh b/src/ci/docker/scripts/musl-toolchain.sh index 25781c747493a..11954b82b8444 100644 --- a/src/ci/docker/scripts/musl-toolchain.sh +++ b/src/ci/docker/scripts/musl-toolchain.sh @@ -41,7 +41,7 @@ cd musl-cross-make hide_output make -j$(nproc) TARGET=$TARGET hide_output make install TARGET=$TARGET OUTPUT=$OUTPUT -cd .. +cd - # Make musl binaries executable @@ -72,5 +72,5 @@ cmake ../libunwind-release_$LLVM \ hide_output make -j$(nproc) cp lib/libunwind.a $OUTPUT/$TARGET/lib -cd ../ && rm -rf libunwind-build +cd - && rm -rf libunwind-build From a7463d4dd212d367ee6c95ec54fab67394e4ec1e Mon Sep 17 00:00:00 2001 From: Martell Malone Date: Sat, 5 Jan 2019 14:34:36 -0800 Subject: [PATCH 12/16] try not having static crt as default --- src/librustc_target/spec/linux_musl_base.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_target/spec/linux_musl_base.rs b/src/librustc_target/spec/linux_musl_base.rs index bcb242bdf4f39..d1610dfd5361b 100644 --- a/src/librustc_target/spec/linux_musl_base.rs +++ b/src/librustc_target/spec/linux_musl_base.rs @@ -26,7 +26,7 @@ pub fn opts() -> TargetOptions { base.post_link_objects_crt.push("crtn.o".to_string()); // These targets statically link libc by default - base.crt_static_default = true; + base.crt_static_default = false; // These targets allow the user to choose between static and dynamic linking. base.crt_static_respected = true; From 8e02f15f076f482c439a9098f5a97eb8a6dfd5a0 Mon Sep 17 00:00:00 2001 From: Martell Malone Date: Sun, 6 Jan 2019 07:53:30 -0800 Subject: [PATCH 13/16] use rcrt1 for pie and make static default again --- src/librustc_codegen_llvm/back/link.rs | 7 ++----- src/librustc_target/spec/linux_musl_base.rs | 7 +++++-- src/librustc_target/spec/mod.rs | 5 +++++ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/librustc_codegen_llvm/back/link.rs b/src/librustc_codegen_llvm/back/link.rs index fc744201a332c..840027cb72251 100644 --- a/src/librustc_codegen_llvm/back/link.rs +++ b/src/librustc_codegen_llvm/back/link.rs @@ -916,13 +916,10 @@ fn link_args(cmd: &mut dyn Linker, let mut position_independent_executable = false; if t.options.position_independent_executables { - let empty_vec = Vec::new(); - let args = sess.opts.cg.link_args.as_ref().unwrap_or(&empty_vec); - let more_args = &sess.opts.cg.link_arg; - let mut args = args.iter().chain(more_args.iter()).chain(used_link_args.iter()); + let static_pie = t.options.static_position_independent_executables; if get_reloc_model(sess) == llvm::RelocMode::PIC - && !sess.crt_static() && !args.any(|x| *x == "-static") { + && (!sess.crt_static() || static_pie) { position_independent_executable = true; } } diff --git a/src/librustc_target/spec/linux_musl_base.rs b/src/librustc_target/spec/linux_musl_base.rs index d1610dfd5361b..9434ede7e0f9c 100644 --- a/src/librustc_target/spec/linux_musl_base.rs +++ b/src/librustc_target/spec/linux_musl_base.rs @@ -21,15 +21,18 @@ pub fn opts() -> TargetOptions { // // Each target directory for musl has these object files included in it so // they'll be included from there. - base.pre_link_objects_exe_crt.push("crt1.o".to_string()); + base.pre_link_objects_exe_crt.push("rcrt1.o".to_string()); base.pre_link_objects_exe_crt.push("crti.o".to_string()); base.post_link_objects_crt.push("crtn.o".to_string()); // These targets statically link libc by default - base.crt_static_default = false; + base.crt_static_default = true; // These targets allow the user to choose between static and dynamic linking. base.crt_static_respected = true; + // Static position-independent executables are supported. + base.static_position_independent_executables = true; + // Defaults for dynamic linking base.dynamic_linking = true; base.executables = true; diff --git a/src/librustc_target/spec/mod.rs b/src/librustc_target/spec/mod.rs index f42b0a1c3c958..8b486815267fc 100644 --- a/src/librustc_target/spec/mod.rs +++ b/src/librustc_target/spec/mod.rs @@ -580,6 +580,8 @@ pub struct TargetOptions { /// the functions in the executable are not randomized and can be used /// during an exploit of a vulnerability in any code. pub position_independent_executables: bool, + /// Support for static position independent executables. + pub static_position_independent_executables: bool, /// Determines if the target always requires using the PLT for indirect /// library calls or not. This controls the default value of the `-Z plt` flag. pub needs_plt: bool, @@ -735,6 +737,7 @@ impl Default for TargetOptions { has_rpath: false, no_default_libraries: true, position_independent_executables: false, + static_position_independent_executables: false, needs_plt: false, relro_level: RelroLevel::None, pre_link_objects_exe: Vec::new(), @@ -1035,6 +1038,7 @@ impl Target { key!(has_rpath, bool); key!(no_default_libraries, bool); key!(position_independent_executables, bool); + key!(static_position_independent_executables, bool); key!(needs_plt, bool); key!(relro_level, RelroLevel)?; key!(archive_format); @@ -1246,6 +1250,7 @@ impl ToJson for Target { target_option_val!(has_rpath); target_option_val!(no_default_libraries); target_option_val!(position_independent_executables); + target_option_val!(static_position_independent_executables); target_option_val!(needs_plt); target_option_val!(relro_level); target_option_val!(archive_format); From 95f7250f0bdf5eb067cb6eb7eb9e8957365abc6f Mon Sep 17 00:00:00 2001 From: Martell Malone Date: Sun, 6 Jan 2019 09:08:53 -0800 Subject: [PATCH 14/16] allow dylibs in static and remove crt specifics --- src/librustc_target/spec/linux_musl_base.rs | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/librustc_target/spec/linux_musl_base.rs b/src/librustc_target/spec/linux_musl_base.rs index 9434ede7e0f9c..d64c8146a8f6f 100644 --- a/src/librustc_target/spec/linux_musl_base.rs +++ b/src/librustc_target/spec/linux_musl_base.rs @@ -3,33 +3,20 @@ use spec::{LinkerFlavor, TargetOptions, RelroLevel}; pub fn opts() -> TargetOptions { let mut base = super::linux_base::opts(); - // Make sure that the linker/gcc really don't pull in anything, including - // default objects, libs, etc. - base.pre_link_args_crt.insert(LinkerFlavor::Gcc, Vec::new()); - base.pre_link_args_crt.get_mut(&LinkerFlavor::Gcc).unwrap().push("-nostdlib".to_string()); - // At least when this was tested, the linker would not add the // `GNU_EH_FRAME` program header to executables generated, which is required // when unwinding to locate the unwinding information. I'm not sure why this // argument is *not* necessary for normal builds, but it can't hurt! base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-Wl,--eh-frame-hdr".to_string()); - // When generating a statically linked executable there's generally some - // small setup needed which is listed in these files. These are provided by - // a musl toolchain and are linked by default by the `musl-gcc` script. Note - // that `gcc` also does this by default, it just uses some different files. - // - // Each target directory for musl has these object files included in it so - // they'll be included from there. - base.pre_link_objects_exe_crt.push("rcrt1.o".to_string()); - base.pre_link_objects_exe_crt.push("crti.o".to_string()); - base.post_link_objects_crt.push("crtn.o".to_string()); - // These targets statically link libc by default base.crt_static_default = true; + // These targets allow the user to choose between static and dynamic linking. base.crt_static_respected = true; + base.crt_static_allows_dylibs = true; + // Static position-independent executables are supported. base.static_position_independent_executables = true; From 2df55a05e763875ca9b50316aaa9b009fe4b4fac Mon Sep 17 00:00:00 2001 From: Martell Malone Date: Sun, 6 Jan 2019 11:20:05 -0800 Subject: [PATCH 15/16] Add -fPIC to CFLAGS --- src/ci/docker/scripts/musl-toolchain.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ci/docker/scripts/musl-toolchain.sh b/src/ci/docker/scripts/musl-toolchain.sh index 11954b82b8444..eca3b2776c56f 100644 --- a/src/ci/docker/scripts/musl-toolchain.sh +++ b/src/ci/docker/scripts/musl-toolchain.sh @@ -51,6 +51,7 @@ echo $OUTPUT/$TARGET/lib >> /etc/ld-musl-$ARCH.path export CC=$TARGET-gcc export CXX=$TARGET-g++ +export CFLAGS="-fPIC $CFLAGS" LLVM=60 From 89f381710f43d169c51b314ac671698ecf24787a Mon Sep 17 00:00:00 2001 From: Martell Malone Date: Sun, 6 Jan 2019 11:45:29 -0800 Subject: [PATCH 16/16] pass -fPIC by default to the linker --- src/librustc_target/spec/linux_musl_base.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/librustc_target/spec/linux_musl_base.rs b/src/librustc_target/spec/linux_musl_base.rs index d64c8146a8f6f..b1e6aff15363e 100644 --- a/src/librustc_target/spec/linux_musl_base.rs +++ b/src/librustc_target/spec/linux_musl_base.rs @@ -3,6 +3,8 @@ use spec::{LinkerFlavor, TargetOptions, RelroLevel}; pub fn opts() -> TargetOptions { let mut base = super::linux_base::opts(); + base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-fPIC".to_string()); + // At least when this was tested, the linker would not add the // `GNU_EH_FRAME` program header to executables generated, which is required // when unwinding to locate the unwinding information. I'm not sure why this