Skip to content

Use a linker script for libstdc++. #1134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changes/1063-1125.json → .changes/1063-1125-1134.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"type": "fixed",
"description": "always link to libc when compiling alpine since static libstdc++ may have undefined references to libc symbols.",
"description": "use a linker script for musl libstdc++ to ensure the archive links to libc, libm, and libgcc as needed.",
"issues": [1124]
}
]
7 changes: 3 additions & 4 deletions docker/Dockerfile.aarch64-unknown-linux-musl
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ ENV CROSS_SYSROOT=/usr/local/aarch64-linux-musl
COPY musl-symlink.sh /
RUN /musl-symlink.sh $CROSS_SYSROOT aarch64

COPY aarch64-linux-musl-gcc.sh /usr/bin/
COPY rustc_info.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
COPY qemu-runner base-runner.sh /

ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
Expand All @@ -33,4 +31,5 @@ ENV CARGO_TARGET_AARCH64_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.
CXX_aarch64_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"g++ \
BINDGEN_EXTRA_CLANG_ARGS_aarch64_unknown_linux_musl="--sysroot=$CROSS_SYSROOT" \
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
RUST_TEST_THREADS=1
RUST_TEST_THREADS=1 \
CROSS_BUILTINS_PATCHED_MINOR_VERSION=48
4 changes: 1 addition & 3 deletions docker/Dockerfile.arm-unknown-linux-musleabi
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT arm

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABI_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABI_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABI_RUNNER="/qemu-runner arm" \
AR_arm_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_arm_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
4 changes: 1 addition & 3 deletions docker/Dockerfile.arm-unknown-linux-musleabihf
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT armhf

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_ARM_UNKNOWN_LINUX_MUSLEABIHF_RUNNER="/qemu-runner armhf" \
AR_arm_unknown_linux_musleabihf="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_arm_unknown_linux_musleabihf="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
7 changes: 3 additions & 4 deletions docker/Dockerfile.armv5te-unknown-linux-musleabi
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ ENV CROSS_SYSROOT=/usr/local/arm-linux-musleabi
COPY musl-symlink.sh /
RUN /musl-symlink.sh $CROSS_SYSROOT arm

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
COPY qemu-runner base-runner.sh /

COPY rust-1-65-musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
COPY rustc_info.sh /

ENV CARGO_TARGET_ARMV5TE_UNKNOWN_LINUX_MUSLEABI_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
CARGO_TARGET_ARMV5TE_UNKNOWN_LINUX_MUSLEABI_RUNNER="/qemu-runner arm" \
AR_armv5te_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_armv5te_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"gcc \
CXX_armv5te_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"g++ \
BINDGEN_EXTRA_CLANG_ARGS_armv5te_unknown_linux_musleabi="--sysroot=$CROSS_SYSROOT" \
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
RUST_TEST_THREADS=1
RUST_TEST_THREADS=1 \
CROSS_BUILTINS_PATCHED_MINOR_VERSION=65
4 changes: 1 addition & 3 deletions docker/Dockerfile.armv7-unknown-linux-musleabi
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT arm

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABI_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABI_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABI_RUNNER="/qemu-runner armv7" \
AR_armv7_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_armv7_unknown_linux_musleabi="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
4 changes: 1 addition & 3 deletions docker/Dockerfile.armv7-unknown-linux-musleabihf
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT armhf

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_ARMV7_UNKNOWN_LINUX_MUSLEABIHF_RUNNER="/qemu-runner armv7hf" \
AR_armv7_unknown_linux_musleabihf="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_armv7_unknown_linux_musleabihf="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
4 changes: 1 addition & 3 deletions docker/Dockerfile.i586-unknown-linux-musl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT i386

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_I586_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_I586_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_I586_UNKNOWN_LINUX_MUSL_RUNNER="/qemu-runner i586" \
AR_i586_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_i586_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
4 changes: 1 addition & 3 deletions docker/Dockerfile.i686-unknown-linux-musl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT i386

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_I686_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_I686_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_I686_UNKNOWN_LINUX_MUSL_RUNNER="/qemu-runner i686" \
AR_i686_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_i686_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
4 changes: 1 addition & 3 deletions docker/Dockerfile.mips-unknown-linux-musl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT mips-sf

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_MIPS_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_MIPS_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_MIPS_UNKNOWN_LINUX_MUSL_RUNNER="/qemu-runner mips" \
AR_mips_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_mips_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
7 changes: 3 additions & 4 deletions docker/Dockerfile.mips64-unknown-linux-muslabi64
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ RUN mkdir -p $CROSS_SYSROOT/usr/lib64
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so $CROSS_SYSROOT/usr/lib64/libc.so
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so.1 $CROSS_SYSROOT/usr/lib64/libc.so.1

COPY rust-1-65-musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
COPY rustc_info.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
COPY qemu-runner base-runner.sh /

ENV CARGO_TARGET_MIPS64_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
Expand All @@ -40,4 +38,5 @@ ENV CARGO_TARGET_MIPS64_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFIX"
CXX_mips64_unknown_linux_muslabi64="$CROSS_TOOLCHAIN_PREFIX"g++ \
BINDGEN_EXTRA_CLANG_ARGS_mips64_unknown_linux_muslabi64="--sysroot=$CROSS_SYSROOT" \
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
RUST_TEST_THREADS=1
RUST_TEST_THREADS=1 \
CROSS_BUILTINS_PATCHED_MINOR_VERSION=65
7 changes: 3 additions & 4 deletions docker/Dockerfile.mips64el-unknown-linux-muslabi64
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ RUN mkdir -p $CROSS_SYSROOT/usr/lib64
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so $CROSS_SYSROOT/usr/lib64/libc.so
RUN ln -s $CROSS_SYSROOT/usr/lib/libc.so.1 $CROSS_SYSROOT/usr/lib64/libc.so.1

COPY rust-1-65-musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
COPY rustc_info.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh
COPY qemu-runner base-runner.sh /

ENV CARGO_TARGET_MIPS64EL_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
Expand All @@ -40,4 +38,5 @@ ENV CARGO_TARGET_MIPS64EL_UNKNOWN_LINUX_MUSLABI64_LINKER="$CROSS_TOOLCHAIN_PREFI
CXX_mips64el_unknown_linux_muslabi64="$CROSS_TOOLCHAIN_PREFIX"g++ \
BINDGEN_EXTRA_CLANG_ARGS_mips64el_unknown_linux_muslabi64="--sysroot=$CROSS_SYSROOT" \
QEMU_LD_PREFIX="$CROSS_SYSROOT" \
RUST_TEST_THREADS=1
RUST_TEST_THREADS=1 \
CROSS_BUILTINS_PATCHED_MINOR_VERSION=65
4 changes: 1 addition & 3 deletions docker/Dockerfile.mipsel-unknown-linux-musl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT mipsel-sf

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_MIPSEL_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_MIPSEL_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_MIPSEL_UNKNOWN_LINUX_MUSL_RUNNER="/qemu-runner mipsel" \
AR_mipsel_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_mipsel_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
4 changes: 1 addition & 3 deletions docker/Dockerfile.x86_64-unknown-linux-musl
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ RUN /musl-symlink.sh $CROSS_SYSROOT x86_64

COPY qemu-runner base-runner.sh /

COPY musl-gcc.sh /usr/bin/"$CROSS_TOOLCHAIN_PREFIX"gcc.sh

ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc.sh \
ENV CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER="$CROSS_TOOLCHAIN_PREFIX"gcc \
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_RUNNER="/qemu-runner x86_64" \
AR_x86_64_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"ar \
CC_x86_64_unknown_linux_musl="$CROSS_TOOLCHAIN_PREFIX"gcc \
Expand Down
37 changes: 0 additions & 37 deletions docker/aarch64-linux-musl-gcc.sh

This file was deleted.

68 changes: 61 additions & 7 deletions docker/musl-gcc.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,74 @@
#!/bin/bash

# this works around missing libc routines when compiling
# against a static libstdc++, which we always do on musl after
# https://github.com/cross-rs/cross/issues/902. the reason
# otherwise, we are missing crucial routines as as `setlocale`,
# `__cxa_atexit`, and others.
# this linker works around missing builtins in older rust versions.
# we also have custom linker scripts for our static libstdc++ for all versions
# which is found in `musl-symlink.sh`.
#
# for other targets, issues in older versions of compiler-builtins require
# manually linking to libgcc to compensate for missing builtins.
# target-specific details include:
#
# aarch64-unknown-linux-musl (fixed in 1.48)
# https://github.com/rust-lang/compiler-builtins/pull/377
#
# armv5te-unknown-linux-musleabi (fixed in 1.65)
# missing sync `sync_X_and_fetch`
# https://github.com/rust-lang/compiler-builtins/pull/484
#
# mips64-unknown-linux-muslabi64, mips64el-unknown-linux-muslabi64 (fixed in 1.65)
# missing soft-fp routine `__trunctfsf2`
# https://github.com/rust-lang/compiler-builtins/pull/483

set -x
set -euo pipefail

main() {
if [[ $# -eq 0 ]]; then
local minor
local patched_minor="${CROSS_BUILTINS_PATCHED_MINOR_VERSION:-0}"
minor=$(rustc_minor_version)

if [[ $# -eq 0 ]] || [[ "${minor}" -ge "${patched_minor}" ]]; then
exec "${CROSS_TOOLCHAIN_PREFIX}"gcc "${@}"
else
exec "${CROSS_TOOLCHAIN_PREFIX}"gcc "${@}" -lc
exec "${CROSS_TOOLCHAIN_PREFIX}"gcc "${@}" -lgcc -static-libgcc
fi
}

# FIXME: the rest of the contents of this file can be removed later on,
# especially after 0.3.0 has been released so we can ensure everyone is
# using a cross version at least as recent as images requiring the rust
# versions provided as environment variables. these functions are wrappers
# around these environment variables for backwards compatibility.
# https://github.com/cross-rs/cross/issues/1046

# NOTE: this will fail if rustc does not provide version
# info, which may happen with a custom toolchain.
rustc_version() {
rustc -Vv | grep '^release:' | cut -d ':' -f2
}

rustc_major_version() {
if [[ -z "${CROSS_RUSTC_MAJOR_VERSION:-}" ]]; then
CROSS_RUSTC_MAJOR_VERSION=$(rustc_version | cut -d '.' -f1)
export CROSS_RUSTC_MAJOR_VERSION
fi
echo "${CROSS_RUSTC_MAJOR_VERSION}"
}

rustc_minor_version() {
if [[ -z "${CROSS_RUSTC_MINOR_VERSION:-}" ]]; then
CROSS_RUSTC_MINOR_VERSION=$(rustc_version | cut -d '.' -f2)
export CROSS_RUSTC_MINOR_VERSION
fi
echo "${CROSS_RUSTC_MINOR_VERSION}"
}

rustc_patch_version() {
if [[ -z "${CROSS_RUSTC_PATCH_VERSION:-}" ]]; then
CROSS_RUSTC_PATCH_VERSION=$(rustc_version | cut -d '.' -f3)
export CROSS_RUSTC_PATCH_VERSION
fi
echo "${CROSS_RUSTC_PATCH_VERSION}"
}

main "${@}"
19 changes: 19 additions & 0 deletions docker/musl-symlink.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ main() {
# https://github.com/cross-rs/cross/issues/902
rm "${sysroot}"/lib/libstdc++.so* || true

# now, we create a linker script that adds all the required dependencies
# because we link to a static libstdc++ to avoid runtime issues and
# with the shared libstdc++, we can have missing symbols that are reference
# in libstdc++, such as those from libc like `setlocale` and `__cxa_atexit`,
# as well as those from libgcc, like `__extendsftf2`. all musl targets
# can require symbols from libc, however, only the following are known
# to require symbols from libgcc:
# - aarch64-unknown-linux-musl
# - mips64-unknown-linux-muslabi64
# - mips64el-unknown-linux-muslabi64
echo '/* cross-rs linker script
* this allows us to statically link libstdc++ to avoid segfaults
* https://github.com/cross-rs/cross/issues/902
*/
GROUP ( libstdc++.a AS_NEEDED( -lgcc -lc -lm ) )
' > "${sysroot}"/lib/libstdc++.so.6.0.27
ln -s libstdc++.so.6.0.27 "${sysroot}"/lib/libstdc++.so.6
ln -s libstdc++.so.6.0.27 "${sysroot}"/lib/libstdc++.so

echo "${sysroot}/lib" >> "/etc/ld-musl-${arch}.path"

rm -rf "${0}"
Expand Down
35 changes: 0 additions & 35 deletions docker/rust-1-65-musl-gcc.sh

This file was deleted.

Loading