crossgcc: Upgrade GCC from 11.4.0 to 13.2.0

Changelogs:
  * https://gcc.gnu.org/gcc-12/changes.html
  * https://gcc.gnu.org/gcc-13/changes.html

Porting guides:
  * https://gcc.gnu.org/gcc-12/porting_to.html
  * https://gcc.gnu.org/gcc-13/porting_to.html

Change-Id: I4f2ed4de4811abaa13528906de71eee29a8f2910
Signed-off-by: Elyes Haouas <ehaouas@noos.fr>
Signed-off-by: Nico Huber <nico.h@gmx.de>
Signed-off-by: Felix Singer <felixsinger@posteo.net>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/70771
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Martin L Roth <gaumless@gmail.com>
diff --git a/util/crossgcc/buildgcc b/util/crossgcc/buildgcc
index 8de6d69..23a5caf 100755
--- a/util/crossgcc/buildgcc
+++ b/util/crossgcc/buildgcc
@@ -38,7 +38,7 @@
 GMP_VERSION=6.3.0
 MPFR_VERSION=4.2.1
 MPC_VERSION=1.3.1
-GCC_VERSION=11.4.0
+GCC_VERSION=13.2.0
 BINUTILS_VERSION=2.41
 IASL_VERSION="R06_28_23"
 # CLANG version number
diff --git a/util/crossgcc/patches/gcc-11.4.0_ada-musl_workaround.patch b/util/crossgcc/patches/gcc-11.4.0_ada-musl_workaround.patch
deleted file mode 100644
index 1f504a4..0000000
--- a/util/crossgcc/patches/gcc-11.4.0_ada-musl_workaround.patch
+++ /dev/null
@@ -1,120 +0,0 @@
-diff --git a/gcc/ada/adaint.c b/gcc/ada/adaint.c
-index 3b0aea92c05..998f2c66289 100644
---- a/gcc/ada/adaint.c
-+++ b/gcc/ada/adaint.c
-@@ -107,6 +107,15 @@
- #define xmalloc(S) malloc (S)
- #define xrealloc(V,S) realloc (V,S)
- #else
-+#if !defined(__ANDROID__) && defined(__linux__)
-+#ifdef __cplusplus
-+extern "C" {
-+#endif
-+#include <sched.h>
-+#ifdef __cplusplus
-+}
-+#endif
-+#endif
- #include "config.h"
- #include "system.h"
- #include "version.h"
-@@ -3265,7 +3274,7 @@ __gnat_lwp_self (void)
- 
- /* Dynamic cpu sets */
- 
--cpu_set_t *
-+void *
- __gnat_cpu_alloc (size_t count)
- {
-   return CPU_ALLOC (count);
-@@ -3278,33 +3287,33 @@ __gnat_cpu_alloc_size (size_t count)
- }
- 
- void
--__gnat_cpu_free (cpu_set_t *set)
-+__gnat_cpu_free (void *set)
- {
--  CPU_FREE (set);
-+  CPU_FREE ((cpu_set_t *)set);
- }
- 
- void
--__gnat_cpu_zero (size_t count, cpu_set_t *set)
-+__gnat_cpu_zero (size_t count, void *set)
- {
-   CPU_ZERO_S (count, set);
- }
- 
- void
--__gnat_cpu_set (int cpu, size_t count, cpu_set_t *set)
-+__gnat_cpu_set (int cpu, size_t count, void *set)
- {
-   /* Ada handles CPU numbers starting from 1, while C identifies the first
-      CPU by a 0, so we need to adjust. */
--  CPU_SET_S (cpu - 1, count, set);
-+  CPU_SET_S (cpu - 1, count, (cpu_set_t *)set);
- }
- 
- #else /* !CPU_ALLOC */
- 
- /* Static cpu sets */
- 
--cpu_set_t *
-+void *
- __gnat_cpu_alloc (size_t count ATTRIBUTE_UNUSED)
- {
--  return (cpu_set_t *) xmalloc (sizeof (cpu_set_t));
-+  return xmalloc (sizeof (cpu_set_t));
- }
- 
- size_t
-@@ -3314,23 +3323,23 @@ __gnat_cpu_alloc_size (size_t count ATTRIBUTE_UNUSED)
- }
- 
- void
--__gnat_cpu_free (cpu_set_t *set)
-+__gnat_cpu_free (void *set)
- {
-   free (set);
- }
- 
- void
--__gnat_cpu_zero (size_t count ATTRIBUTE_UNUSED, cpu_set_t *set)
-+__gnat_cpu_zero (size_t count ATTRIBUTE_UNUSED, void *set)
- {
-   CPU_ZERO (set);
- }
- 
- void
--__gnat_cpu_set (int cpu, size_t count ATTRIBUTE_UNUSED, cpu_set_t *set)
-+__gnat_cpu_set (int cpu, size_t count ATTRIBUTE_UNUSED, void *set)
- {
-   /* Ada handles CPU numbers starting from 1, while C identifies the first
-      CPU by a 0, so we need to adjust. */
--  CPU_SET (cpu - 1, set);
-+  CPU_SET (cpu - 1, (cpu_set_t *)set);
- }
- #endif /* !CPU_ALLOC */
- #endif /* __linux__ */
-diff --git a/gcc/ada/adaint.h b/gcc/ada/adaint.h
-index 0d12f7e9020..7b3def03fb0 100644
---- a/gcc/ada/adaint.h
-+++ b/gcc/ada/adaint.h
-@@ -316,13 +316,11 @@ extern void   *__gnat_lwp_self			   (void);
- 
- /* Routines for interface to required CPU set primitives */
- 
--#include <sched.h>
--
--extern cpu_set_t *__gnat_cpu_alloc                 (size_t);
-+extern void *__gnat_cpu_alloc                 (size_t);
- extern size_t __gnat_cpu_alloc_size                (size_t);
--extern void   __gnat_cpu_free                  (cpu_set_t *);
--extern void   __gnat_cpu_zero                      (size_t, cpu_set_t *);
--extern void   __gnat_cpu_set                       (int, size_t, cpu_set_t *);
-+extern void   __gnat_cpu_free                  (void *);
-+extern void   __gnat_cpu_zero                      (size_t, void *);
-+extern void   __gnat_cpu_set                       (int, size_t, void *);
- #endif
- 
- #if defined (_WIN32)
diff --git a/util/crossgcc/patches/gcc-11.4.0_gnat.patch b/util/crossgcc/patches/gcc-11.4.0_gnat.patch
deleted file mode 100644
index c22cec4..0000000
--- a/util/crossgcc/patches/gcc-11.4.0_gnat.patch
+++ /dev/null
@@ -1,41 +0,0 @@
---- gcc-6.1.0/gcc/ada/gcc-interface/Make-lang.in.bak	2015-08-24 16:23:25.004493665 +0200
-+++ gcc-6.1.0/gcc/ada/gcc-interface/Make-lang.in	2015-08-24 17:53:52.496636113 +0200
-@@ -45,7 +45,7 @@
- 
- 
- # Extra flags to pass to recursive makes.
--COMMON_ADAFLAGS= -gnatpg
-+COMMON_ADAFLAGS= -gnatpg -gnatwn
- ifeq ($(TREECHECKING),)
- CHECKING_ADAFLAGS=
- else
-diff -Nurp gcc-11.2.0/gcc/ada/gcc-interface/Make-lang.in gcc-11.2.0.new/gcc/ada/gcc-interface/Make-lang.in
---- gcc-11.2.0/gcc/ada/gcc-interface/Make-lang.in	2022-06-03 00:31:57.993273717 +0200
-+++ gcc-11.2.0.new/gcc/ada/gcc-interface/Make-lang.in	2022-06-03 00:30:50.214166847 +0200
-@@ -334,6 +334,7 @@ GNAT_ADA_OBJS =	\
-  ada/hostparm.o	\
-  ada/impunit.o	\
-  ada/inline.o	\
-+ ada/libgnat/i-c.o	\
-  ada/libgnat/interfac.o	\
-  ada/itypes.o	\
-  ada/krunch.o	\
-@@ -364,7 +365,10 @@ GNAT_ADA_OBJS =	\
-  ada/rtsfind.o	\
-  ada/libgnat/s-addope.o	\
-  ada/libgnat/s-addima.o \
-+ ada/libgnat/s-aotase.o	\
-  ada/libgnat/s-assert.o	\
-+ ada/libgnat/s-atoope.o	\
-+ ada/libgnat/s-atopri.o	\
-  ada/libgnat/s-bitops.o	\
-  ada/libgnat/s-carun8.o	\
-  ada/libgnat/s-casuti.o	\
-@@ -548,6 +552,7 @@ GNATBIND_OBJS = \
-  ada/hostparm.o   \
-  ada/init.o       \
-  ada/initialize.o \
-+ ada/libgnat/i-c.o        \
-  ada/libgnat/interfac.o   \
-  ada/krunch.o     \
-  ada/lib.o        \
diff --git a/util/crossgcc/patches/gcc-11.4.0_asan_shadow_offset_callback.patch b/util/crossgcc/patches/gcc-13.2.0_asan_shadow_offset_callback.patch
similarity index 99%
rename from util/crossgcc/patches/gcc-11.4.0_asan_shadow_offset_callback.patch
rename to util/crossgcc/patches/gcc-13.2.0_asan_shadow_offset_callback.patch
index 25963b3..d446025 100644
--- a/util/crossgcc/patches/gcc-11.4.0_asan_shadow_offset_callback.patch
+++ b/util/crossgcc/patches/gcc-13.2.0_asan_shadow_offset_callback.patch
@@ -35,8 +35,8 @@
 
 diff --git a/gcc/asan.c b/gcc/asan.c
 index 235e21947..713bf994d 100644
---- a/gcc/asan.c
-+++ b/gcc/asan.c
+--- a/gcc/asan.cc
++++ b/gcc/asan.cc
 @@ -1389,13 +1389,28 @@ asan_emit_stack_protection (rtx base, rtx pbase, unsigned int alignb,
    TREE_ASM_WRITTEN (decl) = 1;
    TREE_ASM_WRITTEN (id) = 1;
diff --git a/util/crossgcc/patches/gcc-13.2.0_gnat.patch b/util/crossgcc/patches/gcc-13.2.0_gnat.patch
new file mode 100644
index 0000000..2a37b15
--- /dev/null
+++ b/util/crossgcc/patches/gcc-13.2.0_gnat.patch
@@ -0,0 +1,108 @@
+From b1b16478729aea709e7ef81224995b6d26b38a87 Mon Sep 17 00:00:00 2001
+From: Nico Huber <nico.h@gmx.de>
+Date: Sun, 16 Jul 2023 00:30:03 +0200
+Subject: [PATCH] GNAT: Build cross compilers with host runtime
+
+When building with an older host toolchain, the shipped libgnat
+sources may not be compatible. So rely on the host runtime just
+like we do for a stage1 build.
+
+Signed-off-by: Nico Huber <nico.h@gmx.de>
+---
+ gcc/ada/gcc-interface/Make-lang.in | 16 +++++++++++++---
+ gcc/ada/init.c                     |  4 ++++
+ 2 files changed, 17 insertions(+), 3 deletions(-)
+
+diff --git a/gcc/ada/gcc-interface/Make-lang.in b/gcc/ada/gcc-interface/Make-lang.in
+index 9507f2f09203..cf7ec4cc1662 100644
+--- a/gcc/ada/gcc-interface/Make-lang.in
++++ b/gcc/ada/gcc-interface/Make-lang.in
+@@ -57,18 +57,21 @@ WARN_ADAFLAGS= -W -Wall
+ # checks fully active.
+ 
+ STAGE1=False
++HOST_RT=False
+ GNATBIND_FLAGS=
+ GNATLIB=
+ 
++ADAFLAGS=$(COMMON_ADAFLAGS) -gnatwns
+ ifeq ($(CROSS),)
+-  ADAFLAGS=$(COMMON_ADAFLAGS) -gnatwns
+ 
+   ifeq ($(if $(wildcard ../stage_current),$(shell cat ../stage_current),stage1),stage1)
+     STAGE1=True
+     GNATBIND_FLAGS=-t
++    HOST_RT=True
+   endif
+ else
+-  ADAFLAGS=$(COMMON_ADAFLAGS)
++  GNATBIND_FLAGS=-t
++  HOST_RT=True
+ endif
+ 
+ ALL_ADAFLAGS = \
+@@ -87,7 +89,7 @@ ifeq ($(strip $(filter-out hpux%,$(host_os))),)
+   STAGE1_LIBS=/usr/lib/libcl.a
+ endif
+ 
+-ifeq ($(STAGE1),True)
++ifeq ($(HOST_RT),True)
+   ADA_INCLUDES=$(COMMON_ADA_INCLUDES)
+   adalib=$(dir $(shell $(CC) -print-libgcc-file-name))adalib
+   GNATLIB=$(adalib)/$(if $(wildcard $(adalib)/libgnat.a),libgnat.a,libgnat.so) $(STAGE1_LIBS)
+@@ -485,6 +487,7 @@ GNAT1_C_OBJS+=    \
+  ada/rtinit.o     \
+  ada/seh_init.o
+ 
++ifeq ($(HOST_RT),False)
+ GNAT_ADA_OBJS+= \
+  ada/gcc-interface/system.o \
+  ada/libgnat/a-assert.o	\
+@@ -555,6 +558,7 @@ GNAT_ADA_OBJS+= \
+  ada/libgnat/s-wchstw.o	\
+  ada/libgnat/s-widuns.o
+ endif
++endif
+ 
+ # Object files for gnat executables
+ GNAT1_ADA_OBJS = $(GNAT_ADA_OBJS) ada/back_end.o ada/gnat1drv.o
+@@ -656,6 +660,9 @@ GNATBIND_OBJS +=  \
+  ada/rtfinal.o    \
+  ada/rtinit.o     \
+  ada/seh_init.o   \
++
++ifeq ($(HOST_RT),False)
++GNATBIND_OBJS +=  \
+  ada/gcc-interface/system.o \
+  ada/libgnat/a-assert.o   \
+  ada/libgnat/a-elchha.o   \
+@@ -707,6 +714,9 @@ GNATBIND_OBJS +=  \
+  ada/libgnat/s-wchjis.o   \
+  ada/libgnat/s-wchstw.o   \
+  ada/libgnat/s-widuns.o   \
++
++endif
++GNATBIND_OBJS +=  \
+  ada/adaint.o     \
+  ada/argv.o       \
+  ada/cio.o        \
+diff --git a/gcc/ada/init.c b/gcc/ada/init.c
+index 5212a38490d3..5ae2efd32ef3 100644
+--- a/gcc/ada/init.c
++++ b/gcc/ada/init.c
+@@ -93,8 +93,12 @@ extern struct Exception_Data storage_error;
+ #ifdef CERT
+ #define Raise_From_Signal_Handler __gnat_raise_exception
+ #else
++#if __GNUC__ < 12
++#define Raise_From_Signal_Handler ada__exceptions__raise_from_signal_handler
++#else
+ #define Raise_From_Signal_Handler __gnat_raise_from_signal_handler
+ #endif
++#endif
+ 
+ extern void Raise_From_Signal_Handler (struct Exception_Data *, const void *)
+   ATTRIBUTE_NORETURN;
+-- 
+2.39.0
+
diff --git a/util/crossgcc/patches/gcc-11.4.0_libcpp.patch b/util/crossgcc/patches/gcc-13.2.0_libcpp.patch
similarity index 85%
rename from util/crossgcc/patches/gcc-11.4.0_libcpp.patch
rename to util/crossgcc/patches/gcc-13.2.0_libcpp.patch
index df28c01..2b0c953 100644
--- a/util/crossgcc/patches/gcc-11.4.0_libcpp.patch
+++ b/util/crossgcc/patches/gcc-13.2.0_libcpp.patch
@@ -1,8 +1,8 @@
 GCC with `-Wformat-security -Werror=format-security` hardening options enabled
 by default rejects some codes in libcpp. This patch fixes them.
 
---- gcc-8.3.0/libcpp/expr.c.bak	2020-09-11 15:44:45.770000000 +0900
-+++ gcc-8.3.0/libcpp/expr.c	2020-09-11 15:46:22.370000000 +0900
+--- gcc-8.3.0/libcpp/expr.cc.bak	2020-09-11 15:44:45.770000000 +0900
++++ gcc-8.3.0/libcpp/expr.cc	2020-09-11 15:46:22.370000000 +0900
 @@ -794,10 +794,10 @@
 
  	  if (CPP_OPTION (pfile, c99))
@@ -16,8 +16,8 @@
          }
 
        result |= CPP_N_INTEGER;
---- gcc-8.3.0/libcpp/macro.c.bak	2020-09-11 16:01:42.550000000 +0900
-+++ gcc-8.3.0/libcpp/macro.c	2020-09-11 16:03:47.850000000 +0900
+--- gcc-8.3.0/libcpp/macro.cc.bak	2020-09-11 16:01:42.550000000 +0900
++++ gcc-8.3.0/libcpp/macro.cc	2020-09-11 16:03:47.850000000 +0900
 @@ -160,7 +160,7 @@
  	if (m_state == 2 && token->type == CPP_PASTE)
  	  {
diff --git a/util/crossgcc/patches/gcc-11.4.0_libgcc.patch b/util/crossgcc/patches/gcc-13.2.0_libgcc.patch
similarity index 100%
rename from util/crossgcc/patches/gcc-11.4.0_libgcc.patch
rename to util/crossgcc/patches/gcc-13.2.0_libgcc.patch
diff --git a/util/crossgcc/patches/gcc-13.2.0_musl_poisoned_calloc.patch b/util/crossgcc/patches/gcc-13.2.0_musl_poisoned_calloc.patch
new file mode 100644
index 0000000..552a7e7
--- /dev/null
+++ b/util/crossgcc/patches/gcc-13.2.0_musl_poisoned_calloc.patch
@@ -0,0 +1,122 @@
+Musl's <sched.h> uses calloc() which is marked as poisoned by GCC's
+"system.h". Work around that by making sure that <sched.h> gets
+included first.
+
+--- gcc-13.2.0/gcc/ada/adaint.c
++++ gcc-13.2.0.musl/gcc/ada/adaint.c
+@@ -101,6 +101,10 @@
+ #include <sys/time.h>
+ #endif
+ 
++#if defined (__linux__)
++#include <sched.h>
++#endif
++
+ #ifdef IN_RTS
+ 
+ #ifdef STANDALONE
+@@ -3441,7 +3445,6 @@
+ #endif
+ 
+ #if defined (__linux__)
+-#include <sched.h>
+ 
+ /* glibc versions earlier than 2.7 do not define the routines to handle
+    dynamically allocated CPU sets. For these targets, we use the static
+--- gcc-13.2.0/gcc/ada/argv.c
++++ gcc-13.2.0.musl/gcc/ada/argv.c
+@@ -42,6 +42,8 @@
+    main program, and these routines are accessed from the
+    Ada.Command_Line.Environment package.  */
+ 
++#include "adaint.h"
++
+ #ifdef IN_RTS
+ #include "runtime.h"
+ #include <stdlib.h>
+@@ -50,8 +52,6 @@
+ #include "config.h"
+ #include "system.h"
+ #endif
+-
+-#include "adaint.h"
+ 
+ #ifdef __cplusplus
+ extern "C" {
+--- gcc-13.2.0/gcc/ada/cio.c
++++ gcc-13.2.0.musl/gcc/ada/cio.c
+@@ -29,6 +29,8 @@
+  *                                                                          *
+  ****************************************************************************/
+ 
++#include "adaint.h"
++
+ #ifdef IN_RTS
+ #include "runtime.h"
+ #include <sys/stat.h>
+@@ -36,8 +38,6 @@
+ #include "config.h"
+ #include "system.h"
+ #endif
+-
+-#include "adaint.h"
+ 
+ /* We need L_tmpnam definition */
+ #include <stdio.h>
+--- gcc-13.2.0/gcc/ada/cstreams.c
++++ gcc-13.2.0.musl/gcc/ada/cstreams.c
+@@ -58,14 +58,14 @@
+ #include "vxWorks.h"
+ #endif
+ 
++#include "adaint.h"
++
+ #ifdef IN_RTS
+ #include <string.h>
+ #else
+ #include "config.h"
+ #include "system.h"
+ #endif
+-
+-#include "adaint.h"
+ 
+ #ifdef __cplusplus
+ extern "C" {
+--- gcc-13.2.0/gcc/ada/init.c
++++ gcc-13.2.0.musl/gcc/ada/init.c
+@@ -53,6 +53,8 @@
+ #undef __linux__
+ #endif
+ 
++#include "adaint.h"
++
+ #ifdef IN_RTS
+ 
+ #ifdef STANDALONE
+@@ -71,7 +73,6 @@
+ #include "system.h"
+ #endif
+ 
+-#include "adaint.h"
+ #include "raise.h"
+ 
+ #ifdef __cplusplus
+--- gcc-13.2.0/gcc/ada/raise.c
++++ gcc-13.2.0.musl/gcc/ada/raise.c
+@@ -32,6 +32,8 @@
+ /* Shared routines to support exception handling.  __gnat_unhandled_terminate
+    is shared between all exception handling mechanisms.  */
+ 
++#include "adaint.h"
++
+ #ifdef IN_RTS
+ #include "runtime.h"
+ #else
+@@ -39,7 +41,6 @@
+ #include "system.h"
+ #endif
+ 
+-#include "adaint.h"
+ #include "raise.h"
+ 
+ #ifdef __cplusplus
diff --git a/util/crossgcc/patches/gcc-11.4.0_rv32iafc.patch b/util/crossgcc/patches/gcc-13.2.0_rv32iafc.patch
similarity index 100%
rename from util/crossgcc/patches/gcc-11.4.0_rv32iafc.patch
rename to util/crossgcc/patches/gcc-13.2.0_rv32iafc.patch
diff --git a/util/crossgcc/sum/gcc-11.4.0.tar.xz.cksum b/util/crossgcc/sum/gcc-11.4.0.tar.xz.cksum
deleted file mode 100644
index c9469c3..0000000
--- a/util/crossgcc/sum/gcc-11.4.0.tar.xz.cksum
+++ /dev/null
@@ -1 +0,0 @@
-03f21dce9edf9092e38b4e23dd27b29f6ab56f63  tarballs/gcc-11.4.0.tar.xz
diff --git a/util/crossgcc/sum/gcc-13.2.0.tar.xz.cksum b/util/crossgcc/sum/gcc-13.2.0.tar.xz.cksum
new file mode 100644
index 0000000..dcf9d41
--- /dev/null
+++ b/util/crossgcc/sum/gcc-13.2.0.tar.xz.cksum
@@ -0,0 +1 @@
+5f95b6d042fb37d45c6cbebfc91decfbc4fb493c  tarballs/gcc-13.2.0.tar.xz