Skip to contentSkip to conversation

arm64: Fix compiling with ancient compiler

PATCH Applied Andrew Jones 2 participants 3 messages Feb 3, 2022 – Feb 9, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 arm64: Fix compiling with ancient compiler

When compiling with an ancient compiler (gcc-4.8.5-36.el7_6.2.aarch64) the build fails with

lib/libcflat.a(alloc.o): In function `mult_overflow':
/home/drjones/kvm-unit-tests/lib/alloc.c:19: undefined reference to `__multi3'

According to kernel commit fb8722735f50 ("arm64: support __int128 on gcc 5+") gcc older than 5 will emit __multi3 for __int128 multiplication. To fix this, let's just use check_mul_overflow(), which does overflow checking with GCC7.1+ and nothing for older gcc. We lose the fallback for older gcc, but oh, well, the heavily negative diffstat is just too tempting to go for another solution.

While we're cleaning up lib/alloc.c with the function deletion also take the opportunity to clean up the include style and add an SPDX header.

---
 lib/alloc.c | 41 ++++++-----------------------------------
 1 file changed, 6 insertions(+), 35 deletions(-)
lib/alloc.c +6 -35
index f4266f5d064e..51d774ddf5df 100644
@@ -1,48 +1,19 @@
-#include "alloc.h"
-#include "asm/page.h"
-#include "bitops.h"
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#include <alloc.h>
+#include <bitops.h>
+#include <asm/page.h>
+#include <linux/compiler.h>
 
 void *malloc(size_t size)
 {
 	return memalign(sizeof(long), size);
 }
 
-static bool mult_overflow(size_t a, size_t b)
-{
-#if BITS_PER_LONG == 32
-	/* 32 bit system, easy case: just use u64 */
-	return (u64)a * (u64)b >= (1ULL << 32);
-#else
-#ifdef __SIZEOF_INT128__
-	/* if __int128 is available use it (like the u64 case above) */
-	unsigned __int128 res = a;
-	res *= b;
-	res >>= 64;
-	return res != 0;
-#else
-	u64 tmp;
-
-	if ((a >> 32) && (b >> 32))
-		return true;
-	if (!(a >> 32) && !(b >> 32))
-		return false;
-	tmp = (u32)a;
-	tmp *= (u32)b;
-	tmp >>= 32;
-	if (a < b)
-		tmp += a * (b >> 32);
-	else
-		tmp += b * (a >> 32);
-	return tmp >> 32;
-#endif /* __SIZEOF_INT128__ */
-#endif /* BITS_PER_LONG == 32 */
-}
-
 void *calloc(size_t nmemb, size_t size)
 {
 	void *ptr;
 
-	assert(!mult_overflow(nmemb, size));
+	assert(!check_mul_overflow(nmemb, size));
 	ptr = malloc(nmemb * size);
 	if (ptr)
 		memset(ptr, 0, nmemb * size);
Signature · 1 line
2.34.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Andrew Jones

When compiling with an ancient compiler (gcc-4.8.5-36.el7_6.2.aarch64) the build fails with

lib/libcflat.a(alloc.o): In function `mult_overflow':
/home/drjones/kvm-unit-tests/lib/alloc.c:19: undefined reference to `__multi3'

Quoted 6 lines

According to kernel commit fb8722735f50 ("arm64: support __int128 on gcc 5+") gcc older than 5 will emit __multi3 for __int128 multiplication. To fix this, let's just use check_mul_overflow(), which does overflow checking with GCC7.1+ and nothing for older gcc. We lose the fallback for older gcc, but oh, well, the heavily negative diffstat is just too tempting to go for another solution.

Sounds ok to me, too.

author Applied
#2 lore raw
In reply to Andrew Jones
Quoted 13 lines · Andrew Jones

When compiling with an ancient compiler (gcc-4.8.5-36.el7_6.2.aarch64) the build fails with

lib/libcflat.a(alloc.o): In function `mult_overflow':
/home/drjones/kvm-unit-tests/lib/alloc.c:19: undefined reference to `__multi3'

According to kernel commit fb8722735f50 ("arm64: support __int128 on gcc 5+") gcc older than 5 will emit __multi3 for __int128 multiplication. To fix this, let's just use check_mul_overflow(), which does overflow checking with GCC7.1+ and nothing for older gcc. We lose the fallback for older gcc, but oh, well, the heavily negative diffstat is just too tempting to go for another solution.

Quoted 8 lines

While we're cleaning up lib/alloc.c with the function deletion also take the opportunity to clean up the include style and add an SPDX header.

---
 lib/alloc.c | 41 ++++++-----------------------------------
 1 file changed, 6 insertions(+), 35 deletions(-)

Applied to misc/queue
https://gitlab.com/rhdrjones/kvm-unit-tests/-/commits/misc/queue

Thanks,
drew