]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/nolibc: add alloca()
authorThomas Weißschuh <linux@weissschuh.net>
Thu, 9 Apr 2026 15:55:28 +0000 (17:55 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Mon, 27 Apr 2026 07:00:50 +0000 (09:00 +0200)
Add the wide-used alloca() function. As it is highly machine and
compiler dependent, just defer to the compiler builtin. This has
been available since GCC 4 and clang 3.

Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://patch.msgid.link/20260409-nolibc-alloca-v1-1-ed02f68dfaf9@weissschuh.net
tools/include/nolibc/Makefile
tools/include/nolibc/alloca.h [new file with mode: 0644]
tools/include/nolibc/nolibc.h
tools/testing/selftests/nolibc/nolibc-test.c

index f0e6e71e83355f3ef46fdd8587420329b2cd4675..872c318f50d41a949e4b29ba37a8a683b2026b78 100644 (file)
@@ -20,6 +20,7 @@ OUTPUT ?= $(CURDIR)/
 architectures := arm arm64 loongarch m68k mips powerpc riscv s390 sh sparc x86
 arch_files := arch.h $(addsuffix .h, $(addprefix arch-, $(architectures)))
 all_files := \
+               alloca.h \
                assert.h \
                byteswap.h \
                compiler.h \
diff --git a/tools/include/nolibc/alloca.h b/tools/include/nolibc/alloca.h
new file mode 100644 (file)
index 0000000..448233a
--- /dev/null
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * alloca() for NOLIBC
+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
+ */
+
+/* make sure to include all global symbols */
+#include "nolibc.h"
+
+#ifndef _NOLIBC_ALLOCA_H
+#define _NOLIBC_ALLOCA_H
+
+#define alloca(size) __builtin_alloca(size)
+
+#endif /* _NOLIBC_ALLOCA_H */
index 4b99795d7a65c2fc1666b49d6ed3241de873c6c3..faa94f24728165033cef815b5868755a95aef52b 100644 (file)
 #include "byteswap.h"
 #include "endian.h"
 #include "assert.h"
+#include "alloca.h"
 
 /* Used by programs to avoid std includes */
 #define NOLIBC
index d3c4facb54c079749282dc64bc0b4f7589019fca..cfb797bf416c7802f81401be60f4c1e47837f5e7 100644 (file)
@@ -45,6 +45,7 @@
 #include <stdbool.h>
 #include <byteswap.h>
 #include <endian.h>
+#include <alloca.h>
 
 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
 
@@ -1516,6 +1517,18 @@ int run_syscall(int min, int max)
        return ret;
 }
 
+int test_alloca(void)
+{
+       uint64_t *x;
+
+       x = alloca(sizeof(*x));
+
+       *x = 0x1234;
+       __asm__ ("" : "+r" (x));
+
+       return *x - 0x1234;
+}
+
 int test_difftime(void)
 {
        if (difftime(200., 100.) != 100.)
@@ -1731,6 +1744,7 @@ int run_stdlib(int min, int max)
                CASE_TEST(toupper_noop);            EXPECT_EQ(1, toupper('A'), 'A'); break;
                CASE_TEST(abs);                     EXPECT_EQ(1, abs(-10), 10); break;
                CASE_TEST(abs_noop);                EXPECT_EQ(1, abs(10), 10); break;
+               CASE_TEST(alloca);                  EXPECT_ZR(1, test_alloca()); break;
                CASE_TEST(difftime);                EXPECT_ZR(1, test_difftime()); break;
                CASE_TEST(memchr_foobar6_o);        EXPECT_STREQ(1, memchr("foobar", 'o', 6), "oobar"); break;
                CASE_TEST(memchr_foobar3_b);        EXPECT_STRZR(1, memchr("foobar", 'b', 3)); break;