From 918fff1af323281c80c4e26343e7eedc614f975a Mon Sep 17 00:00:00 2001 From: Chongyun Lee <45286352+licy183@users.noreply.github.com> Date: Sat, 25 May 2024 19:51:24 +0800 Subject: [PATCH] GCC 14.1 --- .github/workflows/build.yml | 66 ++++++ .gitignore | 11 + build.sh | 163 +++++++++++++ common-files/setup_toolchain_ndk_r17c.sh | 45 ++++ common-files/termux_download.sh | 60 +++++ .../0001-android-related-modifications.patch | 223 ++++++++++++++++++ patches/0002-fix-missing-_L.patch | 25 ++ patches/0003-disable-call-ssp-local.patch | 19 ++ patches/0004-workaround-for-libcxx.patch | 29 +++ 9 files changed, 641 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore create mode 100755 build.sh create mode 100644 common-files/setup_toolchain_ndk_r17c.sh create mode 100755 common-files/termux_download.sh create mode 100644 patches/0001-android-related-modifications.patch create mode 100644 patches/0002-fix-missing-_L.patch create mode 100644 patches/0003-disable-call-ssp-local.patch create mode 100644 patches/0004-workaround-for-libcxx.patch diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..1e95c77 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,66 @@ +name: Build Toolchain +on: + push: + branches: + - main + tags: + - v** + pull_request: +jobs: + build: + name: Build Toolchain + runs-on: ubuntu-22.04 + strategy: + matrix: + target_arch: [aarch64, arm, i686, x86_64] + steps: + - name: Checkout Repo + uses: actions/checkout@main + with: + path: ${{ github.workspace }} + - name: Build Toolchain + env: + TOOLCHAIN_ARCH: ${{ matrix.target_arch }} + run: | + bash ./build.sh + - name: Waiting for debugger + if: ${{ failure() }} + uses: mxschmitt/action-tmate@v3 + - name: Upload Build Archive + uses: actions/upload-artifact@v2 + with: + name: newer-toolchain-${{ matrix.target_arch }}-${{ github.sha }} + path: ${{ github.workspace }}/build/ + release: + name: Create Github Release + if: ${{ github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') }} + needs: build + runs-on: ubuntu-22.04 + steps: + - name: Fetch archives + uses: actions/download-artifact@v2 + with: + path: ./ + - name: Copy archives + run: | + cp newer-toolchain-*-${{ github.sha }}/*.tar.bz2 ./ + - name: Get checksums + id: checksums + run: | + checksums=$(printf 'SHA-256:\n```\n%s\n```\n' "$(sha256sum *.tar.bz2)") + checksums="${checksums//'%'/'%25'}" + checksums="${checksums//$'\n'/'%0A'}" + checksums="${checksums//$'\r'/'%0D'}" + echo "::set-output name=checksums::$checksums" + - name: Get tag + id: tag + uses: dawidd6/action-get-tag@v1 + - name: Publish GitHub release + uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: "*.tar.bz2" + file_glob: true + release_name: "Android NDK toolchain version r17c with gcc" + tag: ${{ steps.tag.outputs.tag }} + body: ${{ steps.checksums.outputs.checksums }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a89dd9f --- /dev/null +++ b/.gitignore @@ -0,0 +1,11 @@ +# Cache files +/cache/ + +# Temp files +/tmp/ + +# Build files +/build/ + +# VSCode +/.vscode/ diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..c718c21 --- /dev/null +++ b/build.sh @@ -0,0 +1,163 @@ +#!/bin/bash +# build.sh - script to build a custom NDK toolchain +# +# Copyright 2022 Chongyun Lee +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +set -e -u -o pipefail + +_SCRIPTDIR=$(cd "$(realpath "$(dirname "$0")")"; pwd) +source $_SCRIPTDIR/common-files/setup_toolchain_ndk_r17c.sh +source $_SCRIPTDIR/common-files/termux_download.sh + +: ${TOOLCHAIN_ARCH:=aarch64} +: ${_CACHE_DIR:=$_SCRIPTDIR/cache} +: ${_TMP_DIR:=$_SCRIPTDIR/tmp} +: ${_API_LEVEL:=21} +: ${_MAKE_PROCESSES:=$(nproc)} +: ${GCC_VERSION:=14.1.0} +: ${GCC_SHA256:=a0be066c02775002a0fa65ad3c65fb56a8bfd923d072a26ed148c0439ecdb68f} +: ${BINUTILS_VERSION:=2.42} +: ${BINUTILS_SHA256:=5d2a6c1d49686a557869caae08b6c2e83699775efd27505e01b2f4db1a024ffc} + +export TOOLCHAIN_ARCH + +TERMUX_PKG_TMPDIR=$_TMP_DIR +mkdir -p $_CACHE_DIR +rm -rf $_TMP_DIR +mkdir -p $_TMP_DIR + +_HOST_PLATFORM="${TOOLCHAIN_ARCH}-linux-android" + +_GCC_EXTRA_HOST_BUILD="" +_BINUTILS_EXTRA_HOST_BUILD="--enable-gold" +if [ "$TOOLCHAIN_ARCH" = "arm" ]; then + _HOST_PLATFORM="${_HOST_PLATFORM}eabi" + _GCC_EXTRA_HOST_BUILD="--with-arch=armv7-a --with-float=soft --with-fpu=vfp" +elif [ "$TOOLCHAIN_ARCH" = "aarch64" ]; then + _GCC_EXTRA_HOST_BUILD="--enable-fix-cortex-a53-835769 --enable-fix-cortex-a53-843419" + _BINUTILS_EXTRA_HOST_BUILD+=" $_GCC_EXTRA_HOST_BUILD" +elif [ "$TOOLCHAIN_ARCH" = "i686" ]; then + _GCC_EXTRA_HOST_BUILD="--with-arch=i686 --with-fpmath=sse " +elif [ "$TOOLCHAIN_ARCH" = "x86_64" ]; then + _GCC_EXTRA_HOST_BUILD="--with-arch=x86-64 --with-fpmath=sse" +fi + +# Install dependencies +sudo apt update +sudo apt install -y build-essential curl +sudo apt install -y libgmp-dev libmpfr-dev libmpc-dev zlib1g-dev libisl-dev libtinfo5 libncurses5 + +pushd $_TMP_DIR + +# Download source +GCC_SRC_URL=https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz +GCC_SRC_FILE=$_CACHE_DIR/gcc-${GCC_VERSION}.tar.gz +GCC_SRC_DIR=$_TMP_DIR/gcc-${GCC_VERSION} +termux_download $GCC_SRC_URL $GCC_SRC_FILE $GCC_SHA256 +BINUTILS_SRC_URL=https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.gz +BINUTILS_SRC_FILE=$_CACHE_DIR/binutils-${BINUTILS_VERSION}.tar.gz +BINUTILS_SRC_DIR=$_TMP_DIR/binutils-${BINUTILS_VERSION} +termux_download $BINUTILS_SRC_URL $BINUTILS_SRC_FILE $BINUTILS_SHA256 + +# Setup a standalone toolchain +_setup_standalone_toolchain_ndk_r17c $_TMP_DIR/standalone-toolchain +cp -R $_TMP_DIR/standalone-toolchain/sysroot/usr/include/$_HOST_PLATFORM/* $_TMP_DIR/standalone-toolchain/sysroot/usr/include/ + +# Extract source +tar -xf $GCC_SRC_FILE -C $_TMP_DIR/ +pushd $_TMP_DIR +PATCHES="$(find "$_SCRIPTDIR/patches/" -maxdepth 1 -type f -name *.patch | sort)" +for f in $PATCHES; do + echo "Applying patch: $(basename $f)" + patch -d "$GCC_SRC_DIR/" -p1 < "$f"; +done +tar -xf $BINUTILS_SRC_FILE -C $_TMP_DIR/ +popd + +# Copy sysroot +mkdir -p $_TMP_DIR/newer-toolchain +cp -R $_TMP_DIR/standalone-toolchain/sysroot $_TMP_DIR/newer-toolchain/ + +# Set CPPFLAGS/CFLAGS/CXXFLAGS +export CPPFLAGS="-O3 -g0" +export CFLAGS="$CPPFLAGS" +export CXXFLAGS="$CPPFLAGS" + +# Build binutils +mkdir -p binutils-build +pushd binutils-build +$BINUTILS_SRC_DIR/configure \ + --target=$_HOST_PLATFORM \ + --prefix=$_TMP_DIR/newer-toolchain \ + --with-sysroot=$_TMP_DIR/newer-toolchain/sysroot \ + $_BINUTILS_EXTRA_HOST_BUILD +make -j $_MAKE_PROCESSES +make -j $_MAKE_PROCESSES install-strip +popd # binutils-build + +export PATH="$_TMP_DIR/newer-toolchain/bin:$PATH" + +# Build GCC toolchain +mkdir -p newer-toolchain-build +pushd newer-toolchain-build + +export CPPFLAGS+=" -D__ANDROID_API__=$_API_LEVEL" +export CFLAGS+=" -D__ANDROID_API__=$_API_LEVEL" +export CXXFLAGS+=" -D__ANDROID_API__=$_API_LEVEL" + +$GCC_SRC_DIR/configure \ + --host=x86_64-linux-gnu \ + --build=x86_64-linux-gnu \ + --target=$_HOST_PLATFORM \ + --disable-shared \ + --disable-nls \ + --enable-default-pie \ + --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' \ + --with-gnu-as --with-gnu-ld \ + --disable-libstdc__-v3 \ + --disable-tls \ + --disable-ssp \ + --disable-bootstrap \ + --enable-initfini-array \ + --enable-libatomic-ifuncs=no \ + --prefix=$_TMP_DIR/newer-toolchain \ + --with-gmp --with-mpfr --with-mpc --with-system-zlib \ + --enable-languages=c,c++,fortran \ + --enable-plugins --enable-libgomp \ + --enable-gnu-indirect-function \ + --disable-libcilkrts --disable-libsanitizer \ + --enable-gold --enable-threads \ + --enable-eh-frame-hdr-for-static \ + --enable-graphite=yes --with-isl \ + --disable-multilib \ + $_GCC_EXTRA_HOST_BUILD \ + --with-sysroot=$_TMP_DIR/newer-toolchain/sysroot \ + --with-gxx-include-dir=$_TMP_DIR/newer-toolchain/include/c++/$GCC_VERSION + +make -j $_MAKE_PROCESSES +make -j $_MAKE_PROCESSES install-strip + +popd # newer-toolchain-build + +# Make the archive +mv newer-toolchain gcc-$GCC_VERSION-$TOOLCHAIN_ARCH +tar -cjvf gcc-$GCC_VERSION-$TOOLCHAIN_ARCH.tar.bz2 gcc-$GCC_VERSION-$TOOLCHAIN_ARCH + +popd # $_TMP_DIR + +# Copy the archive +mkdir -p build +cp $_TMP_DIR/gcc-$GCC_VERSION-$TOOLCHAIN_ARCH.tar.bz2 ./build diff --git a/common-files/setup_toolchain_ndk_r17c.sh b/common-files/setup_toolchain_ndk_r17c.sh new file mode 100644 index 0000000..9eec315 --- /dev/null +++ b/common-files/setup_toolchain_ndk_r17c.sh @@ -0,0 +1,45 @@ +_download_ndk_r17c() { + if [ ! -f $_CACHE_DIR/.placeholder-android-ndk-r17c ]; then + echo "Start downloading Android NDK toolchain (version r17c)..." + mkdir -p $_CACHE_DIR/ + local _NDKARCHIVE_FILE=$_CACHE_DIR/android-ndk-r17c-linux-x86_64.zip + local _NDK_URL=https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip + local _NDK_SHA256=3f541adbd0330a9205ba12697f6d04ec90752c53d6b622101a2a8a856e816589 + termux_download $_NDK_URL $_NDKARCHIVE_FILE $_NDK_SHA256 + unzip -d $_CACHE_DIR/ $_NDKARCHIVE_FILE > /dev/null 2>&1 + touch $_CACHE_DIR/.placeholder-android-ndk-r17c + echo "Downloading completed." + fi +} + +_setup_standalone_toolchain_ndk_r17c() { + _download_ndk_r17c + + local TOOLCHAIN_DIR="$1" + rm -rf $TOOLCHAIN_DIR + + local _NDKARCH + if [ "$TOOLCHAIN_ARCH" == "aarch64" ]; then + _NDKARCH="arm64" + elif [ "$TOOLCHAIN_ARCH" == "arm" ]; then + _NDKARCH="arm" + elif [ "$TOOLCHAIN_ARCH" == "x86_64" ]; then + _NDKARCH="x86_64" + elif [ "$TOOLCHAIN_ARCH" == "i686" ]; then + _NDKARCH="x86" + fi + + # Setup a standalone toolchain + python $_CACHE_DIR/android-ndk-r17c/build/tools/make_standalone_toolchain.py \ + --arch $_NDKARCH --api $_API_LEVEL --install-dir $TOOLCHAIN_DIR + + # Modify sysroot + pushd $TOOLCHAIN_DIR + + # See https://github.com/android/ndk/issues/215#issuecomment-524293090 + sed -i "s/include_next /include /" include/c++/4.9.x/cstddef + + sed -i "s/define __ANDROID_API__ __ANDROID_API_FUTURE__/define __ANDROID_API__ $_API_LEVEL/" \ + sysroot/usr/include/android/api-level.h + popd +} diff --git a/common-files/termux_download.sh b/common-files/termux_download.sh new file mode 100755 index 0000000..6788cc3 --- /dev/null +++ b/common-files/termux_download.sh @@ -0,0 +1,60 @@ +#!/usr/bin/bash +## +## Copyright 2020 Termux +## +## Licensed under the Apache License, Version 2.0 (the "License"); +## you may not use this file except in compliance with the License. +## You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## + +termux_download() { + if [ $# != 3 ]; then + termux_error_exit "termux_download(): Invalid arguments - expected \$URL \$DESTINATION \$CHECKSUM" + fi + local URL="$1" + local DESTINATION="$2" + local CHECKSUM="$3" + + if [ -f "$DESTINATION" ] && [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then + # Keep existing file if checksum matches. + local EXISTING_CHECKSUM + EXISTING_CHECKSUM=$(sha256sum "$DESTINATION" | cut -f 1 -d ' ') + if [ "$EXISTING_CHECKSUM" = "$CHECKSUM" ]; then return; fi + fi + + local TMPFILE + TMPFILE=$(mktemp "$TERMUX_PKG_TMPDIR/download.${TERMUX_PKG_NAME-unnamed}.XXXXXXXXX") + echo "Downloading ${URL}" + if curl --fail --retry 20 --retry-connrefused --retry-delay 30 --location --output "$TMPFILE" "$URL"; then + local ACTUAL_CHECKSUM + ACTUAL_CHECKSUM=$(sha256sum "$TMPFILE" | cut -f 1 -d ' ') + if [ "$CHECKSUM" != "SKIP_CHECKSUM" ]; then + if [ "$CHECKSUM" != "$ACTUAL_CHECKSUM" ]; then + >&2 printf "Wrong checksum for %s:\nExpected: %s\nActual: %s\n" \ + "$URL" "$CHECKSUM" "$ACTUAL_CHECKSUM" + return 1 + fi + elif [ -z "$CHECKSUM" ]; then + printf "WARNING: No checksum check for %s:\nActual: %s\n" \ + "$URL" "$ACTUAL_CHECKSUM" + fi + mv "$TMPFILE" "$DESTINATION" + return 0 + fi + + echo "Failed to download $URL" >&2 + return 1 +} + +# Make script standalone executable as well as sourceable +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + termux_download "$@" +fi diff --git a/patches/0001-android-related-modifications.patch b/patches/0001-android-related-modifications.patch new file mode 100644 index 0000000..ab10033 --- /dev/null +++ b/patches/0001-android-related-modifications.patch @@ -0,0 +1,223 @@ +Based on https://github.com/crystax/android-toolchain-gcc-6/commit/080803512c8f6f87c2f1f711170d54033144d628 and +https://github.com/its-pointless/gcc_termux/tree/5d83a14dfb87cb86b9d47e3c36ead328546f315c/termux-packages/experimental-packages/gcc-6 + +diff -uNr a/gcc/config/aarch64/aarch64-linux-android.h b/gcc/config/aarch64/aarch64-linux-android.h +--- a/gcc/config/aarch64/aarch64-linux-android.h ++++ b/gcc/config/aarch64/aarch64-linux-android.h +@@ -0,0 +1,59 @@ ++/* Machine description for AArch64 architecture. ++ Copyright (C) 2014 Free Software Foundation, Inc. ++ ++ This file is part of GCC. ++ ++ GCC is free software; you can redistribute it and/or modify it ++ under the terms of the GNU General Public License as published by ++ the Free Software Foundation; either version 3, or (at your option) ++ any later version. ++ ++ GCC is distributed in the hope that it will be useful, but ++ WITHOUT ANY WARRANTY; without even the implied warranty of ++ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ++ General Public License for more details. ++ ++ You should have received a copy of the GNU General Public License ++ along with GCC; see the file COPYING3. If not see ++ . */ ++ ++#ifndef GCC_AARCH64_LINUX_ANDROID_H ++#define GCC_AARCH64_LINUX_ANDROID_H ++ ++ ++#undef TARGET_OS_CPP_BUILTINS ++#define TARGET_OS_CPP_BUILTINS() \ ++ do \ ++ { \ ++ GNU_USER_TARGET_OS_CPP_BUILTINS(); \ ++ ANDROID_TARGET_OS_CPP_BUILTINS(); \ ++ } \ ++ while (0) ++ ++#undef LINK_SPEC ++#define LINK_SPEC \ ++ LINUX_OR_ANDROID_LD (LINUX_TARGET_LINK_SPEC, \ ++ LINUX_TARGET_LINK_SPEC " " ANDROID_LINK_SPEC) ++ ++#undef CC1_SPEC ++#define CC1_SPEC \ ++ LINUX_OR_ANDROID_CC (GNU_USER_TARGET_CC1_SPEC, \ ++ GNU_USER_TARGET_CC1_SPEC " " ANDROID_CC1_SPEC("-fpic")) ++ ++#define CC1PLUS_SPEC \ ++ LINUX_OR_ANDROID_CC ("", ANDROID_CC1PLUS_SPEC) ++ ++#undef LIB_SPEC ++#define LIB_SPEC \ ++ LINUX_OR_ANDROID_LD (GNU_USER_TARGET_LIB_SPEC, \ ++ GNU_USER_TARGET_NO_PTHREADS_LIB_SPEC " " ANDROID_LIB_SPEC) ++ ++#undef STARTFILE_SPEC ++#define STARTFILE_SPEC \ ++ LINUX_OR_ANDROID_LD (GNU_USER_TARGET_STARTFILE_SPEC, ANDROID_STARTFILE_SPEC) ++ ++#undef ENDFILE_SPEC ++#define ENDFILE_SPEC \ ++ LINUX_OR_ANDROID_LD (GNU_USER_TARGET_ENDFILE_SPEC, ANDROID_ENDFILE_SPEC) ++ ++#endif /* GCC_AARCH64_LINUX_ANDROID_H */ +diff -uNr a/gcc/config/aarch64/aarch64-linux.h b/gcc/config/aarch64/aarch64-linux.h +--- a/gcc/config/aarch64/aarch64-linux.h ++++ b/gcc/config/aarch64/aarch64-linux.h +@@ -26,6 +26,9 @@ + #undef MUSL_DYNAMIC_LINKER + #define MUSL_DYNAMIC_LINKER "/lib/ld-musl-aarch64%{mbig-endian:_be}%{mabi=ilp32:_ilp32}.so.1" + ++#undef BIONIC_DYNAMIC_LINKER ++#define BIONIC_DYNAMIC_LINKER "/system/bin/linker64" ++ + #undef ASAN_CC1_SPEC + #define ASAN_CC1_SPEC "%{%:sanitize(address):-funwind-tables}" + +diff -uNr a/gcc/config/arm/elf.h b/gcc/config/arm/elf.h +--- a/gcc/config/arm/elf.h ++++ b/gcc/config/arm/elf.h +@@ -56,8 +56,7 @@ + #undef SUBSUBTARGET_EXTRA_SPECS + #define SUBSUBTARGET_EXTRA_SPECS + +-#ifndef ASM_SPEC +-#define ASM_SPEC "\ ++#define DEFAULT_ASM_SPEC "\ + %{mbig-endian:-EB} \ + %{mlittle-endian:-EL} \ + %(asm_cpu_spec) \ +@@ -66,6 +65,8 @@ + %{mthumb-interwork:-mthumb-interwork} \ + %{mfloat-abi=*} %{!mfpu=auto: %{mfpu=*}} \ + %(subtarget_extra_asm_spec)" ++#ifndef ASM_SPEC ++#define ASM_SPEC DEFAULT_ASM_SPEC + #endif + + /* The ARM uses @ are a comment character so we need to redefine +diff -uNr a/gcc/config/arm/linux-eabi.h b/gcc/config/arm/linux-eabi.h +--- a/gcc/config/arm/linux-eabi.h ++++ b/gcc/config/arm/linux-eabi.h +@@ -108,11 +108,16 @@ + LINUX_OR_ANDROID_CC (GNU_USER_TARGET_CC1_SPEC " " ASAN_CC1_SPEC " " \ + FDPIC_CC1_SPEC, \ + GNU_USER_TARGET_CC1_SPEC " " ASAN_CC1_SPEC " " \ +- ANDROID_CC1_SPEC) ++ ANDROID_CC1_SPEC("-fpic")) + + #define CC1PLUS_SPEC \ + LINUX_OR_ANDROID_CC ("", ANDROID_CC1PLUS_SPEC) + ++#undef ASM_SPEC ++#define ASM_SPEC \ ++ LINUX_OR_ANDROID_CC (DEFAULT_ASM_SPEC, \ ++ DEFAULT_ASM_SPEC " " ANDROID_ASM_SPEC) ++ + #undef LIB_SPEC + #define LIB_SPEC \ + LINUX_OR_ANDROID_LD (GNU_USER_TARGET_LIB_SPEC, \ +diff -uNr a/gcc/config/i386/gnu-user.h b/gcc/config/i386/gnu-user.h +--- a/gcc/config/i386/gnu-user.h ++++ b/gcc/config/i386/gnu-user.h +@@ -59,9 +59,13 @@ + When the -shared link option is used a final link is not being + done. */ + ++#undef ANDROID_TARGET_CC1_SPEC ++#define ANDROID_TARGET_CC1_SPEC \ ++ " -mssse3 -fno-short-enums " \ ++ + #undef ASM_SPEC + #define ASM_SPEC \ +- "--32 %{msse2avx:%{!mavx:-msse2avx}}" ++ "--32 %{msse2avx:%{!mavx:-msse2avx}}" LINUX_OR_ANDROID_CC ("", ANDROID_ASM_SPEC) + + #undef SUBTARGET_EXTRA_SPECS + #define SUBTARGET_EXTRA_SPECS \ +diff -uNr a/gcc/config/i386/gnu-user64.h b/gcc/config/i386/gnu-user64.h +--- a/gcc/config/i386/gnu-user64.h ++++ b/gcc/config/i386/gnu-user64.h +@@ -46,6 +46,11 @@ + #define SPEC_X32 "mx32" + #endif + ++#undef ANDROID_TARGET_CC1_SPEC ++#define ANDROID_TARGET_CC1_SPEC \ ++ "%{m32:-mssse3 -fno-short-enums}" \ ++ "%{!m32:-msse4.2 -mpopcnt}" ++ + #undef ASM_SPEC + #define ASM_SPEC "%{" SPEC_32 ":--32} \ + %{" SPEC_64 ":--64} \ +diff -uNr a/gcc/config/i386/linux-common.h b/gcc/config/i386/linux-common.h +--- a/gcc/config/i386/linux-common.h ++++ b/gcc/config/i386/linux-common.h +@@ -33,7 +33,13 @@ + #undef CC1_SPEC + #define CC1_SPEC \ + LINUX_OR_ANDROID_CC (GNU_USER_TARGET_CC1_SPEC, \ +- GNU_USER_TARGET_CC1_SPEC " " ANDROID_CC1_SPEC) ++ GNU_USER_TARGET_CC1_SPEC \ ++ ANDROID_TARGET_CC1_SPEC \ ++ " " \ ++ ANDROID_CC1_SPEC("-fPIC")) ++ ++#define CC1PLUS_SPEC \ ++ LINUX_OR_ANDROID_CC ("", ANDROID_CC1PLUS_SPEC) + + #undef LINK_SPEC + #define LINK_SPEC \ +diff -uNr a/gcc/config/linux-android.h b/gcc/config/linux-android.h +--- a/gcc/config/linux-android.h ++++ b/gcc/config/linux-android.h +@@ -44,15 +44,18 @@ + "%{" NOANDROID "|tno-android-ld:" LINUX_SPEC ";:" ANDROID_SPEC "}" + + #define ANDROID_LINK_SPEC \ +- "%{shared: -Bsymbolic}" ++ "%{shared: -Bsymbolic} -z noexecstack -z relro -z now" + +-#define ANDROID_CC1_SPEC \ ++#define ANDROID_CC1_SPEC(ANDROID_PIC_DEFAULT) \ + "%{!mglibc:%{!muclibc:%{!mbionic: -mbionic}}} " \ +- "%{!fno-pic:%{!fno-PIC:%{!fpic:%{!fPIC: -fPIC}}}}" ++ "%{!fno-pic:%{!fno-PIC:%{!fpic:%{!fPIC: " ANDROID_PIC_DEFAULT "}}}}" + + #define ANDROID_CC1PLUS_SPEC \ +- "%{!fexceptions:%{!fno-exceptions: -fno-exceptions}} " \ +- "%{!frtti:%{!fno-rtti: -fno-rtti}}" ++ "%{!fexceptions:%{!fno-exceptions: -fexceptions}} " \ ++ "%{!frtti:%{!fno-rtti: -frtti}}" ++ ++#define ANDROID_ASM_SPEC \ ++ "--noexecstack" + + #define ANDROID_LIB_SPEC \ + "%{!static: -ldl}" +diff -uNr a/gcc/config.gcc b/gcc/config.gcc +--- a/gcc/config.gcc ++++ b/gcc/config.gcc +@@ -1176,14 +1176,18 @@ + extra_options="${extra_options} netbsd.opt netbsd-elf.opt" + ;; + aarch64*-*-linux*) +- tm_file="${tm_file} elfos.h gnu-user.h linux.h glibc-stdint.h" ++ tm_file="${tm_file} elfos.h gnu-user.h linux.h linux-android.h glibc-stdint.h" + tm_file="${tm_file} aarch64/aarch64-elf.h aarch64/aarch64-errata.h aarch64/aarch64-linux.h" ++ extra_options="${extra_options} linux-android.opt" + tmake_file="${tmake_file} aarch64/t-aarch64 aarch64/t-aarch64-linux" + tm_defines="${tm_defines} TARGET_DEFAULT_ASYNC_UNWIND_TABLES=1" + case $target in + aarch64_be-*) + tm_defines="${tm_defines} TARGET_BIG_ENDIAN_DEFAULT=1" + ;; ++ aarch64*-*-linux-android*) ++ tm_file="${tm_file} aarch64/aarch64-linux-android.h" ++ ;; + esac + aarch64_multilibs="${with_multilib_list}" + if test "$aarch64_multilibs" = "default"; then diff --git a/patches/0002-fix-missing-_L.patch b/patches/0002-fix-missing-_L.patch new file mode 100644 index 0000000..05a555b --- /dev/null +++ b/patches/0002-fix-missing-_L.patch @@ -0,0 +1,25 @@ +Based on https://android.googlesource.com/toolchain/gcc/+/44ea170a62f4bb566c5487c446a31300d1e0e319%5E%21/ + +--- a/libstdc++-v3/config/os/bionic/ctype_base.h ++++ b/libstdc++-v3/config/os/bionic/ctype_base.h +@@ -28,6 +28,20 @@ + + // Information as gleaned from /usr/include/ctype.h + ++#if !defined(_U) ++#if !defined(_CTYPE_U) ++#error Bionic header ctype.h does not define either _U nor _CTYPE_U ++#endif ++#define _U _CTYPE_U ++#define _L _CTYPE_L ++#define _N _CTYPE_N ++#define _S _CTYPE_S ++#define _P _CTYPE_P ++#define _C _CTYPE_C ++#define _X _CTYPE_X ++#define _B _CTYPE_B ++#endif ++ + namespace std _GLIBCXX_VISIBILITY(default) + { + _GLIBCXX_BEGIN_NAMESPACE_VERSION diff --git a/patches/0003-disable-call-ssp-local.patch b/patches/0003-disable-call-ssp-local.patch new file mode 100644 index 0000000..0aaa9ed --- /dev/null +++ b/patches/0003-disable-call-ssp-local.patch @@ -0,0 +1,19 @@ +GCC uses a wrapper of function `__stack_chk_fail` (named `__stack_chk_fail_local`) +to optimize for PIC code on x86 architecture, but this function is now removed from +the prebuilt crtbegin.o, so we should make GCC NOT call this wrapper. + +[1]: https://android.googlesource.com/platform/bionic/+/5501003be73b73de59044b44b12f6e20ba6e0021 +[2]: https://android.googlesource.com/platform/bionic/+/8f7120b/libc/arch-x86/bionic/__stack_chk_fail_local.h +--- a/gcc/config/i386/i386.cc ++++ b/gcc/config/i386/i386.cc +@@ -21810,9 +21810,7 @@ + static tree ATTRIBUTE_UNUSED + ix86_stack_protect_fail (void) + { +- return TARGET_64BIT +- ? default_external_stack_protect_fail () +- : default_hidden_stack_protect_fail (); ++ return default_external_stack_protect_fail (); + } + + /* Select a format to encode pointers in exception handling data. CODE diff --git a/patches/0004-workaround-for-libcxx.patch b/patches/0004-workaround-for-libcxx.patch new file mode 100644 index 0000000..439de63 --- /dev/null +++ b/patches/0004-workaround-for-libcxx.patch @@ -0,0 +1,29 @@ +--- a/gcc/ginclude/float.h ++++ b/gcc/ginclude/float.h +@@ -25,6 +25,12 @@ + * ISO C Standard: 5.2.4.2.2 Characteristics of floating types + */ + ++#ifdef _LIBCPP_CFLOAT ++#if __has_include_next() ++# include_next ++#endif ++#endif ++ + #ifndef _FLOAT_H___ + #define _FLOAT_H___ + +--- a/gcc/ginclude/stddef.h ++++ b/gcc/ginclude/stddef.h +@@ -24,6 +24,11 @@ + /* + * ISO C Standard: 7.17 Common definitions + */ ++#ifdef _LIBCPP_CSTDDEF ++#if __has_include_next() ++# include_next ++#endif ++#endif + #if (!defined(_STDDEF_H) && !defined(_STDDEF_H_) && !defined(_ANSI_STDDEF_H) \ + && !defined(__STDDEF_H__)) \ + || defined(__need_wchar_t) || defined(__need_size_t) \