Skip to content

Commit 385ef15

Browse files
committed
Auto merge of #47663 - malbarbo:mips-crt-static, r=alexcrichton
Do not assume dynamic linking for musl/mips[el] targets All musl targets except mips[el] assume static linking by default. This can be [confusing](https://users.rust-lang.org/t/static-cross-compiled-binaries-arent-really-static/6084). When the musl/mips[el] targets was [added](#31298), dynamic linking was chosen because of binary size concerns, and probably also because libunwind [didn't](https://users.rust-lang.org/t/static-cross-compiled-binaries-arent-really-static/6084/8) supported mips. Now that we have `crt-static` target-feature (the user can choose dynamic link for musl targets), and libunwind [6.0](https://github.com/llvm-mirror/libunwind/commits/release_60) add support to mips, we do not need to assume dynamic linking.
2 parents 21882aa + 2875f82 commit 385ef15

File tree

12 files changed

+56
-74
lines changed

12 files changed

+56
-74
lines changed

src/bootstrap/compile.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl Step for Std {
8080

8181
// Even if we're not building std this stage, the new sysroot must
8282
// still contain the musl startup objects.
83-
if target.contains("musl") && !target.contains("mips") {
83+
if target.contains("musl") {
8484
let libdir = builder.sysroot_libdir(compiler, target);
8585
copy_musl_third_party_objects(build, target, &libdir);
8686
}
@@ -97,7 +97,7 @@ impl Step for Std {
9797
println!("Building stage{} std artifacts ({} -> {})", compiler.stage,
9898
&compiler.host, target);
9999

100-
if target.contains("musl") && !target.contains("mips") {
100+
if target.contains("musl") {
101101
let libdir = builder.sysroot_libdir(compiler, target);
102102
copy_musl_third_party_objects(build, target, &libdir);
103103
}

src/bootstrap/configure.py

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def v(*args):
120120
"armv7-unknown-linux-musleabihf install directory")
121121
v("musl-root-aarch64", "target.aarch64-unknown-linux-musl.musl-root",
122122
"aarch64-unknown-linux-musl install directory")
123+
v("musl-root-mips", "target.mips-unknown-linux-musl.musl-root",
124+
"mips-unknown-linux-musl install directory")
125+
v("musl-root-mipsel", "target.mipsel-unknown-linux-musl.musl-root",
126+
"mipsel-unknown-linux-musl install directory")
123127
v("qemu-armhf-rootfs", "target.arm-unknown-linux-gnueabihf.qemu-rootfs",
124128
"rootfs in qemu testing, you probably don't want to use this")
125129
v("qemu-aarch64-rootfs", "target.aarch64-unknown-linux-gnu.qemu-rootfs",

src/bootstrap/sanity.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn check(build: &mut Build) {
170170
}
171171

172172
// Make sure musl-root is valid
173-
if target.contains("musl") && !target.contains("mips") {
173+
if target.contains("musl") {
174174
// If this is a native target (host is also musl) and no musl-root is given,
175175
// fall back to the system toolchain in /usr before giving up
176176
if build.musl_root(*target).is_none() && build.config.build == *target {

src/ci/docker/dist-i586-gnu-i586-i686-musl/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1818

1919
WORKDIR /build/
2020
COPY scripts/musl.sh /build/
21-
RUN CC=gcc CFLAGS="-m32 -fPIC -Wa,-mrelax-relocations=no" \
21+
RUN CC=gcc CFLAGS="-m32 -Wa,-mrelax-relocations=no" \
2222
CXX=g++ CXXFLAGS="-m32 -Wa,-mrelax-relocations=no" \
2323
bash musl.sh i686 --target=i686 && \
24-
CC=gcc CFLAGS="-march=pentium -m32 -fPIC -Wa,-mrelax-relocations=no" \
24+
CC=gcc CFLAGS="-march=pentium -m32 -Wa,-mrelax-relocations=no" \
2525
CXX=g++ CXXFLAGS="-march=pentium -m32 -Wa,-mrelax-relocations=no" \
2626
bash musl.sh i586 --target=i586 && \
2727
rm -rf /build

src/ci/docker/dist-various-1/Dockerfile

+21-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ RUN ./build-rumprun.sh
3030
COPY dist-various-1/install-x86_64-redox.sh /build
3131
RUN ./install-x86_64-redox.sh
3232

33+
COPY dist-various-1/install-mips-musl.sh /build
34+
RUN ./install-mips-musl.sh
35+
36+
COPY dist-various-1/install-mipsel-musl.sh /build
37+
RUN ./install-mipsel-musl.sh
38+
39+
# Suppress some warnings in the openwrt toolchains we downloaded
40+
ENV STAGING_DIR=/tmp
41+
3342
COPY scripts/musl.sh /build
3443
RUN env \
3544
CC=arm-linux-gnueabi-gcc CFLAGS="-march=armv6 -marm" \
@@ -47,14 +56,16 @@ RUN env \
4756
CC=aarch64-linux-gnu-gcc \
4857
CXX=aarch64-linux-gnu-g++ \
4958
bash musl.sh aarch64 && \
59+
env \
60+
CC=mips-openwrt-linux-gcc \
61+
CXX=mips-openwrt-linux-g++ \
62+
bash musl.sh mips && \
63+
env \
64+
CC=mipsel-openwrt-linux-gcc \
65+
CXX=mipsel-openwrt-linux-g++ \
66+
bash musl.sh mipsel && \
5067
rm -rf /build/*
5168

52-
COPY dist-various-1/install-mips-musl.sh /build
53-
RUN ./install-mips-musl.sh
54-
55-
COPY dist-various-1/install-mipsel-musl.sh /build
56-
RUN ./install-mipsel-musl.sh
57-
5869
ENV TARGETS=asmjs-unknown-emscripten
5970
ENV TARGETS=$TARGETS,wasm32-unknown-emscripten
6071
ENV TARGETS=$TARGETS,x86_64-rumprun-netbsd
@@ -77,16 +88,15 @@ ENV CC_mipsel_unknown_linux_musl=mipsel-openwrt-linux-gcc \
7788
CC_armv5te_unknown_linux_gnueabi=arm-linux-gnueabi-gcc \
7889
CFLAGS_armv5te_unknown_linux_gnueabi="-march=armv5te -marm -mfloat-abi=soft"
7990

80-
# Suppress some warnings in the openwrt toolchains we downloaded
81-
ENV STAGING_DIR=/tmp
82-
8391
ENV RUST_CONFIGURE_ARGS \
84-
--enable-extended \
8592
--target=$TARGETS \
8693
--musl-root-arm=/musl-arm \
8794
--musl-root-armhf=/musl-armhf \
8895
--musl-root-armv7=/musl-armv7 \
89-
--musl-root-aarch64=/musl-aarch64
96+
--musl-root-aarch64=/musl-aarch64 \
97+
--musl-root-mips=/musl-mips \
98+
--musl-root-mipsel=/musl-mipsel
99+
90100
ENV SCRIPT python2.7 ../x.py dist --target $TARGETS
91101

92102
# sccache

src/ci/docker/dist-x86_64-musl/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ WORKDIR /build/
2121
COPY scripts/musl.sh /build/
2222
# We need to mitigate rust-lang/rust#34978 when compiling musl itself as well
2323
RUN CC=gcc \
24-
CFLAGS="-fPIC -Wa,-mrelax-relocations=no" \
24+
CFLAGS="-Wa,-mrelax-relocations=no" \
2525
CXX=g++ \
2626
CXXFLAGS="-Wa,-mrelax-relocations=no" \
2727
bash musl.sh x86_64 && rm -rf /build

src/ci/docker/scripts/musl.sh

+4-32
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ exit 1
3030
TAG=$1
3131
shift
3232

33+
export CFLAGS="-fPIC $CFLAGS"
34+
3335
MUSL=musl-1.1.18
3436

3537
# may have been downloaded in a previous run
@@ -49,42 +51,12 @@ hide_output make clean
4951

5052
cd ..
5153

52-
LLVM=39
54+
LLVM=60
55+
5356
# may have been downloaded in a previous run
5457
if [ ! -d libunwind-release_$LLVM ]; then
5558
curl -L https://github.com/llvm-mirror/llvm/archive/release_$LLVM.tar.gz | tar xzf -
5659
curl -L https://github.com/llvm-mirror/libunwind/archive/release_$LLVM.tar.gz | tar xzf -
57-
# Whoa what's this mysterious patch we're applying to libunwind! Why are we
58-
# swapping the values of ESP/EBP in libunwind?!
59-
#
60-
# Discovered in #35599 it turns out that the vanilla build of libunwind is not
61-
# suitable for unwinding i686 musl. After some investigation it ended up
62-
# looking like the register values for ESP/EBP were indeed incorrect (swapped)
63-
# in the source. Similar commits in libunwind (r280099 and r282589) have noticed
64-
# this for other platforms, and we just need to realize it for musl linux as
65-
# well.
66-
#
67-
# More technical info can be found at #35599
68-
cd libunwind-release_$LLVM
69-
patch -Np1 << EOF
70-
diff --git a/include/libunwind.h b/include/libunwind.h
71-
index c5b9633..1360eb2 100644
72-
--- a/include/libunwind.h
73-
+++ b/include/libunwind.h
74-
@@ -151,8 +151,8 @@ enum {
75-
UNW_X86_ECX = 1,
76-
UNW_X86_EDX = 2,
77-
UNW_X86_EBX = 3,
78-
- UNW_X86_EBP = 4,
79-
- UNW_X86_ESP = 5,
80-
+ UNW_X86_ESP = 4,
81-
+ UNW_X86_EBP = 5,
82-
UNW_X86_ESI = 6,
83-
UNW_X86_EDI = 7
84-
};
85-
fi
86-
EOF
87-
cd ..
8860
fi
8961

9062
mkdir libunwind-build

src/librustc_back/target/mips_unknown_linux_musl.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
// except according to those terms.
1010

1111
use LinkerFlavor;
12-
use target::{Target, TargetOptions, TargetResult};
12+
use target::{Target, TargetResult};
1313

1414
pub fn target() -> TargetResult {
15+
let mut base = super::linux_musl_base::opts();
16+
base.cpu = "mips32r2".to_string();
17+
base.features = "+mips32r2,+soft-float".to_string();
18+
base.max_atomic_width = Some(32);
19+
// see #36994
20+
base.exe_allocation_crate = None;
21+
base.crt_static_default = false;
1522
Ok(Target {
1623
llvm_target: "mips-unknown-linux-musl".to_string(),
1724
target_endian: "big".to_string(),
@@ -23,15 +30,6 @@ pub fn target() -> TargetResult {
2330
target_env: "musl".to_string(),
2431
target_vendor: "unknown".to_string(),
2532
linker_flavor: LinkerFlavor::Gcc,
26-
options: TargetOptions {
27-
cpu: "mips32r2".to_string(),
28-
features: "+mips32r2,+soft-float".to_string(),
29-
max_atomic_width: Some(32),
30-
31-
// see #36994
32-
exe_allocation_crate: None,
33-
34-
..super::linux_base::opts()
35-
}
33+
options: base,
3634
})
3735
}

src/librustc_back/target/mipsel_unknown_linux_musl.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
// except according to those terms.
1010

1111
use LinkerFlavor;
12-
use target::{Target, TargetOptions, TargetResult};
12+
use target::{Target, TargetResult};
1313

1414
pub fn target() -> TargetResult {
15+
let mut base = super::linux_musl_base::opts();
16+
base.cpu = "mips32".to_string();
17+
base.features = "+mips32,+soft-float".to_string();
18+
base.max_atomic_width = Some(32);
19+
// see #36994
20+
base.exe_allocation_crate = None;
21+
base.crt_static_default = false;
1522
Ok(Target {
1623
llvm_target: "mipsel-unknown-linux-musl".to_string(),
1724
target_endian: "little".to_string(),
@@ -23,15 +30,6 @@ pub fn target() -> TargetResult {
2330
target_env: "musl".to_string(),
2431
target_vendor: "unknown".to_string(),
2532
linker_flavor: LinkerFlavor::Gcc,
26-
options: TargetOptions {
27-
cpu: "mips32".to_string(),
28-
features: "+mips32,+soft-float".to_string(),
29-
max_atomic_width: Some(32),
30-
31-
// see #36994
32-
exe_allocation_crate: None,
33-
34-
..super::linux_base::opts()
35-
}
33+
options: base,
3634
})
3735
}

src/libunwind/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
let target = env::var("TARGET").expect("TARGET was not set");
1616

1717
if target.contains("linux") {
18-
if target.contains("musl") && !target.contains("mips") {
18+
if target.contains("musl") {
1919
// musl is handled in lib.rs
2020
} else if !target.contains("android") {
2121
println!("cargo:rustc-link-lib=gcc_s");

src/libunwind/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ cfg_if! {
3535
}
3636
}
3737

38-
#[cfg(all(target_env = "musl", not(target_arch = "mips")))]
38+
#[cfg(target_env = "musl")]
3939
#[link(name = "unwind", kind = "static", cfg(target_feature = "crt-static"))]
4040
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
4141
extern {}

0 commit comments

Comments
 (0)