From 732134932b7b01262fbcb3e08328ec09af1f90e8 Mon Sep 17 00:00:00 2001
From: Maximilian Brune <maximilian.brune@9elements.com>
Date: Thu, 18 Jan 2024 18:15:17 +0600
Subject: util/crossgcc/buildgcc: Compile RISC-V GCC with medany

currently the HiFive Unmatched mainboard produces the following error:
```
util/crossgcc/xgcc/lib/gcc/riscv64-elf/13.2.0/rv64imafdc/lp64d/libgcc.a
(_clzsi2.o): in function `__clzdi2':
util/crossgcc/gcc-13.2.0/libgcc/libgcc2.c:690:(.text+0x1e): relocation
truncated to fit: R_RISCV_HI20 against symbol `__clz_tab' defined in
.rodata section in util/crossgcc/xgcc/lib/gcc/riscv64-elf/13.2.0/
rv64imafdc/lp64d/libgcc.a(_clz.o)
```

This is due to the fact that the libgcc.a library is compiled with the
medlow code model but the mainboards are compiled with the medany code
model.

Changing the code model of the GCC libraries to the medany code model
fixes the issue.

Signed-off-by: Maximilian Brune <maximilian.brune@9elements.com>
Change-Id: If5f07ce034686dd7fec160ea76838507c0ba7fa0
Reviewed-on: https://review.coreboot.org/c/coreboot/+/80139
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Reviewed-by: ron minnich <rminnich@gmail.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
---
 util/crossgcc/buildgcc | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

(limited to 'util')

diff --git a/util/crossgcc/buildgcc b/util/crossgcc/buildgcc
index 29193769f5..26b4b05b15 100755
--- a/util/crossgcc/buildgcc
+++ b/util/crossgcc/buildgcc
@@ -774,6 +774,11 @@ build_cross_GCC() {
 	[ -n "$CXX" ] && $CXX --version | grep clang >/dev/null 2>&1 && \
 		CLANGCXXFLAGS="-fbracket-depth=1024"
 
+	# standard code model is medlow but all mainboards are compiled with medany code model
+	if [ "${TARGETARCH}" = "riscv64-elf" ]; then
+		CFLAGS_FOR_TARGET_EXTRA="-mcmodel=medany"
+	fi
+
 	# GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
 	# both target and host object files.
 	# There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
@@ -783,7 +788,7 @@ build_cross_GCC() {
 	# using C++.
 	# shellcheck disable=SC2086
 	CC="$(hostcc target)" CXX="$(hostcxx target)" \
-		CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" \
+		CFLAGS_FOR_TARGET="${CFLAGS_FOR_TARGET_EXTRA} -O2 -Dinhibit_libc" \
 		CFLAGS="$HOSTCFLAGS $CLANGFLAGS" \
 		CFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGFLAGS" \
 		CXXFLAGS="$HOSTCFLAGS $CLANGCXXFLAGS" \
-- 
cgit v1.2.3