1 # Build script for gcc.
3 # Copyright (c) 2016-2021 Matias Fonzo, <selk@dragora.org>.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
27 rm -rf gcc-${version} gmp-${gmp_version} \
28 mpfr-${mpfr_version} mpc-${mpc_version} isl-${isl_version}
29 unpack "${worktree}/sources/gcc-${version}.tar.xz" \
30 "${worktree}/sources/gmp-${gmp_version}.tar.lz" \
31 "${worktree}/sources/mpfr-${mpfr_version}.tar.bz2" \
32 "${worktree}/sources/mpc-${mpc_version}.tar.gz" \
33 "${worktree}/sources/isl-${isl_version}.tar.bz2"
38 # Apply specific patches for the support in musl.
39 # https://port70.net/~nsz/musl/gcc-10.3.0/
41 patch -Np1 -i "${worktree}/patches/gcc/10/0002-posix_memalign.patch"
42 patch -Np1 -i "${worktree}/patches/gcc/10/0003-j2.patch"
43 patch -Np1 -i "${worktree}/patches/gcc/10/0004-static-pie.patch"
44 patch -Np1 -i "${worktree}/patches/gcc/10/0005-libstdc-futex-time64.patch"
45 patch -Np1 -i "${worktree}/patches/gcc/10/0006-m68k-sqrt.patch"
47 # Apply extra patches to increment the security
48 patch -Np1 -i "${worktree}/patches/gcc/10/extra-relro-in-dragora.patch"
49 patch -Np1 -i "${worktree}/patches/gcc/10/extra-musl_libssp.patch"
51 # Apply patches from "Alpine Linux" in order to improve the security (Thanks!)
52 patch -Np1 -i "${worktree}/patches/gcc/0004-Turn-on-D_FORTIFY_SOURCE-2-by-default-for-C-C-ObjC-O.patch"
53 patch -Np1 -i "${worktree}/patches/gcc/0005-On-linux-targets-pass-as-needed-by-default-to-the-li.patch"
54 patch -Np1 -i "${worktree}/patches/gcc/0006-Enable-Wformat-and-Wformat-security-by-default.patch"
55 patch -Np1 -i "${worktree}/patches/gcc/0007-Enable-Wtrampolines-by-default.patch"
56 patch -Np1 -i "${worktree}/patches/gcc/0008-Disable-ssp-on-nostdlib-nodefaultlibs-and-ffreestand.patch"
57 patch -Np1 -i "${worktree}/patches/gcc/0020-add-fortify-headers-paths.patch"
59 # Hard code default ld path to look at /tools
61 find . -type f -name '*.h' -exec grep -l "/lib/ld-" {} + | while read -r file
63 test -f "$file" || continue;
64 cp -f "$file" "${file}.orig"
66 sed -e 's@/lib\(64\)\{0,1\}\(32\)\{0,1\}/ld@/tools&@g' \
67 -e 's@/usr@/tools@g' "${file}.orig" > "$file"
69 #undef STANDARD_STARTFILE_PREFIX_1
70 #undef STANDARD_STARTFILE_PREFIX_2
71 #define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
72 #define STANDARD_STARTFILE_PREFIX_2 ""' >> "$file"
75 # Make symlinks for requisites
76 ln -s ../gmp-${gmp_version} gmp
77 ln -s ../mpfr-${mpfr_version} mpfr
78 ln -s ../mpc-${mpc_version} mpc
79 ln -s ../isl-${isl_version} isl
81 # Apply patches for MPFR
85 for file in "${worktree}"/patches/mpfr/*
95 # Update detection for hosts based on musl
97 cp -f "${worktree}/archive/common/config.guess" gmp/configfsf.guess
98 cp -f "${worktree}/archive/common/config.sub" gmp/config.sub
100 cp -f "${worktree}/archive/common/config.guess" isl/config.guess
101 cp -f "${worktree}/archive/common/config.sub" isl/config.sub
103 # Build in a separate directory
108 # Import and export toolchain variables
109 . "${worktree}/stages/env.d/cross-staticenv"
111 ../gcc-${version}/configure \
112 AR="$AR" AS="$AS" LD="$LD" RANLIB="$RANLIB" READELF="$READELF" STRIP="$STRIP" \
113 CC="$BTCC" CXX="$BTCXX" \
114 CFLAGS="$BTCFLAGS" CXXFLAGS="$BTCXXFLAGS" LDFLAGS="$BTLDFLAGS" \
116 --libdir=/tools/lib${libSuffix} \
120 --enable-languages=c,c++ \
121 --enable-clocale=generic \
123 --enable-initfini-array \
125 --enable-libstdcxx-time \
126 --enable-fully-dynamic-string \
127 --enable-default-ssp \
128 --enable-default-pie \
131 --disable-bootstrap \
132 --disable-libstdcxx-pch \
133 --disable-gnu-indirect-function \
134 --disable-libsanitizer \
136 --disable-install-libiberty \
137 --with-linker-hash-style=gnu \
138 --with-local-prefix=/tools \
139 --with-native-system-header-dir=/tools/include \
144 AS_FOR_TARGET="$AS" \
147 make -j${jobs} install
149 # Provide minimal libssp_nonshared.a
151 $BTCC -c "${worktree}/archive/gcc/__stack_chk_fail_local.c" \
152 -o __stack_chk_fail_local.o
153 $AR rc libssp_nonshared.a __stack_chk_fail_local.o
154 $RANLIB libssp_nonshared.a
155 cp -p libssp_nonshared.a /tools/lib${libSuffix}
157 # Unset some imported variables from file
158 unset AR AS LD RANLIB READELF STRIP
164 rm -rf gcc-${version} gcc-build \
165 gmp-${gmp_version} mpfr-${mpfr_version} \
166 mpc-${mpc_version} isl-${isl_version}