[libc-commits] [libc] [libc] Create AArch64BaremetalTestUtils and refactor existing Arm 32-bit file (PR #247228)

via libc-commits libc-commits at lists.llvm.org
Mon Nov 30 08:50:15 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Victor Campos (vhscampos)

<details>
<summary>Changes</summary>

This patch creates a new AArch64BaremetalTestUtils file to contain the symbols necessary for baremetal testing on AArch64.

Since most of the functionality is common between AArch64 and Arm, this patch refactors the common code into a new header file. Only the definitions of `semihosting_call` and of `__llvm_libc_exit` differ.

In CMake, specific targets are created for each component to make them follow better the LLVM libc conventions: the new header file is a header library, and each target-specific source file is a new object library.

---
Full diff: https://github.com/llvm/llvm-project/pull/247228.diff


4 Files Affected:

- (added) libc/test/UnitTest/AArch64BaremetalTestUtils.cpp (+42) 
- (added) libc/test/UnitTest/ArmAArch64BaremetalCommonTestUtils.h (+93) 
- (modified) libc/test/UnitTest/ArmBaremetalTestUtils.cpp (+4-77) 
- (modified) libc/test/UnitTest/CMakeLists.txt (+26-6) 


``````````diff
diff --git a/libc/test/UnitTest/AArch64BaremetalTestUtils.cpp b/libc/test/UnitTest/AArch64BaremetalTestUtils.cpp
new file mode 100644
index 00000000000000..0989873674a4e8
--- /dev/null
+++ b/libc/test/UnitTest/AArch64BaremetalTestUtils.cpp
@@ -0,0 +1,42 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// AArch64 bare-metal hermetic test utilities.
+///
+//===----------------------------------------------------------------------===//
+
+#include "ArmAArch64BaremetalCommonTestUtils.h"
+
+#define SEMIHOST_INSTRUCTION "hlt #0xF000"
+
+namespace {
+long semihosting_call(int val, const void *ptr) {
+  register long v __asm__("x0") = val;
+  register const void *p __asm__("x1") = ptr;
+  __asm__ __volatile__(SEMIHOST_INSTRUCTION
+                       : "+r"(v), "+r"(p)
+                       :
+                       : "memory", "cc");
+  return v;
+}
+} // namespace
+
+extern "C" {
+[[noreturn]] void __llvm_libc_exit(int status) {
+  uint32_t semihosting_status = (status == 0) ? ADP_Stopped_ApplicationExit
+                                              : ADP_Stopped_RunTimeErrorUnknown;
+  const struct {
+    uint64_t code;
+    int64_t status;
+  } args = {static_cast<uint64_t>(semihosting_status),
+            static_cast<int64_t>(status)};
+  semihosting_call(SYS_EXIT, reinterpret_cast<const void *>(&args));
+  __builtin_unreachable(); // This semihosting call does not return.
+}
+} // extern "C"
diff --git a/libc/test/UnitTest/ArmAArch64BaremetalCommonTestUtils.h b/libc/test/UnitTest/ArmAArch64BaremetalCommonTestUtils.h
new file mode 100644
index 00000000000000..17ab597d9d354a
--- /dev/null
+++ b/libc/test/UnitTest/ArmAArch64BaremetalCommonTestUtils.h
@@ -0,0 +1,93 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Bare-metal hermetic test utilities common across Arm and AArch64.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/stdint_proxy.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/ssize_t.h"
+#include "hdr/types/struct_timespec.h"
+
+// Redirect stdout/stderr, time and exit from libc tests to semihosting
+
+namespace {
+// Semihosting constants and semihosting call wrapper.
+// https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst
+constexpr uint32_t SYS_OPEN = 0x01;
+constexpr uint32_t SYS_WRITE = 0x05;
+constexpr uint32_t SYS_CLOCK = 0x10;
+constexpr uint32_t SYS_TIME = 0x11;
+constexpr uint32_t SYS_EXIT = 0x18;
+
+constexpr uint32_t OPENMODE_W = 4;
+
+constexpr uint32_t ADP_Stopped_ApplicationExit = 0x20026;
+constexpr uint32_t ADP_Stopped_RunTimeErrorUnknown = 0x20023;
+
+long semihosting_call(int, const void *);
+} // namespace
+
+extern "C" {
+struct __llvm_libc_stdio_cookie {
+  long handle;
+};
+
+struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
+struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;
+
+static void stdio_open(struct __llvm_libc_stdio_cookie *cookie, size_t mode) {
+  const char std_stream_name[] = ":tt";
+  size_t args[] = {
+      reinterpret_cast<size_t>(std_stream_name),
+      mode,
+      sizeof(std_stream_name) - 1UL,
+  };
+  cookie->handle = semihosting_call(SYS_OPEN, args);
+}
+
+void _platform_init(void) {
+  stdio_open(&__llvm_libc_stdout_cookie, OPENMODE_W);
+  stdio_open(&__llvm_libc_stderr_cookie, OPENMODE_W);
+}
+
+ssize_t __llvm_libc_stdio_write(struct __llvm_libc_stdio_cookie *cookie,
+                                const char *buf, size_t size) {
+  size_t args[] = {
+      static_cast<size_t>(cookie->handle),
+      reinterpret_cast<size_t>(buf),
+      size,
+  };
+  ssize_t retval = semihosting_call(SYS_WRITE, args);
+  if (retval >= 0)
+    retval = size - retval;
+  return retval;
+}
+
+bool __llvm_libc_timespec_get_active(struct timespec *ts) {
+  long retval = semihosting_call(SYS_CLOCK, 0);
+  if (retval == -1)
+    return false;
+
+  // Semihosting uses centiseconds.
+  ts->tv_sec = (retval / 100);
+  ts->tv_nsec = (retval % 100) * (1'000'000'000 / 100);
+  return true;
+}
+
+bool __llvm_libc_timespec_get_utc(struct timespec *ts) {
+  long retval = semihosting_call(SYS_TIME, 0);
+
+  // Semihosting uses seconds.
+  ts->tv_sec = retval;
+  ts->tv_nsec = 0;
+  return true;
+}
+} // extern "C"
diff --git a/libc/test/UnitTest/ArmBaremetalTestUtils.cpp b/libc/test/UnitTest/ArmBaremetalTestUtils.cpp
index 720df0b23342c9..eb7fea789a753f 100644
--- a/libc/test/UnitTest/ArmBaremetalTestUtils.cpp
+++ b/libc/test/UnitTest/ArmBaremetalTestUtils.cpp
@@ -11,26 +11,7 @@
 ///
 //===----------------------------------------------------------------------===//
 
-#include "hdr/stdint_proxy.h"
-#include "hdr/types/size_t.h"
-#include "hdr/types/ssize_t.h"
-#include "hdr/types/struct_timespec.h"
-
-// Redirect stdout/stderr, time and exit from libc tests to semihosting
-
-namespace {
-// Semihosting constants and semihosting call wrapper.
-// https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst
-constexpr uint32_t SYS_OPEN = 0x01;
-constexpr uint32_t SYS_WRITE = 0x05;
-constexpr uint32_t SYS_CLOCK = 0x10;
-constexpr uint32_t SYS_TIME = 0x11;
-constexpr uint32_t SYS_EXIT = 0x18;
-
-constexpr uint32_t OPENMODE_W = 4;
-
-constexpr uint32_t ADP_Stopped_ApplicationExit = 0x20026;
-constexpr uint32_t ADP_Stopped_RunTimeErrorUnknown = 0x20023;
+#include "ArmAArch64BaremetalCommonTestUtils.h"
 
 #if defined(__thumb__) // T32
 #if defined(__ARM_ARCH_PROFILE) && __ARM_ARCH_PROFILE == 'M'
@@ -42,7 +23,8 @@ constexpr uint32_t ADP_Stopped_RunTimeErrorUnknown = 0x20023;
 #define SEMIHOST_INSTRUCTION "svc 0x123456"
 #endif
 
-long semihosting_call(long val, const void *ptr) {
+namespace {
+long semihosting_call(int val, const void *ptr) {
   register long v __asm__("r0") = val;
   register const void *p __asm__("r1") = ptr;
   __asm__ __volatile__(SEMIHOST_INSTRUCTION
@@ -54,66 +36,11 @@ long semihosting_call(long val, const void *ptr) {
 } // namespace
 
 extern "C" {
-struct __llvm_libc_stdio_cookie {
-  int handle;
-};
-
-struct __llvm_libc_stdio_cookie __llvm_libc_stdout_cookie;
-struct __llvm_libc_stdio_cookie __llvm_libc_stderr_cookie;
-
-static void stdio_open(struct __llvm_libc_stdio_cookie *cookie, size_t mode) {
-  const char std_stream_name[] = ":tt";
-  size_t args[] = {
-      reinterpret_cast<size_t>(std_stream_name),
-      mode,
-      sizeof(std_stream_name) - 1UL,
-  };
-  cookie->handle = semihosting_call(SYS_OPEN, args);
-}
-
-void _platform_init(void) {
-  stdio_open(&__llvm_libc_stdout_cookie, OPENMODE_W);
-  stdio_open(&__llvm_libc_stderr_cookie, OPENMODE_W);
-}
-
-void __llvm_libc_exit(int status) {
+[[noreturn]] void __llvm_libc_exit(int status) {
   uint32_t semihosting_status = (status == 0) ? ADP_Stopped_ApplicationExit
                                               : ADP_Stopped_RunTimeErrorUnknown;
   semihosting_call(SYS_EXIT,
                    reinterpret_cast<const void *>(semihosting_status));
   __builtin_unreachable(); // This semihosting call does not return.
 }
-
-ssize_t __llvm_libc_stdio_write(struct __llvm_libc_stdio_cookie *cookie,
-                                const char *buf, size_t size) {
-  size_t args[] = {
-      static_cast<size_t>(cookie->handle),
-      reinterpret_cast<size_t>(buf),
-      size,
-  };
-  ssize_t retval = semihosting_call(SYS_WRITE, args);
-  if (retval >= 0)
-    retval = size - retval;
-  return retval;
-}
-
-bool __llvm_libc_timespec_get_active(struct timespec *ts) {
-  long retval = semihosting_call(SYS_CLOCK, 0);
-  if (retval == -1)
-    return false;
-
-  // Semihosting uses centiseconds.
-  ts->tv_sec = (retval / 100);
-  ts->tv_nsec = (retval % 100) * (1'000'000'000 / 100);
-  return true;
-}
-
-bool __llvm_libc_timespec_get_utc(struct timespec *ts) {
-  long retval = semihosting_call(SYS_TIME, 0);
-
-  // Semihosting uses seconds.
-  ts->tv_sec = retval;
-  ts->tv_nsec = 0;
-  return true;
-}
 } // extern "C"
diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 2a135457400061..892187760d4f33 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -171,17 +171,37 @@ set(libc_hermetic_test_support_deps
   libc.hdr.stdint_proxy
   libc.src.__support.libc_errno
 )
+
 if(LIBC_TARGET_OS_IS_BAREMETAL)
+  if(LIBC_TARGET_ARCHITECTURE_IS_ARM OR LIBC_TARGET_ARCHITECTURE_IS_AARCH64)
+    add_header_library(ArmAArch64BaremetalCommonTestUtils
+      HDRS
+        ArmAArch64BaremetalCommonTestUtils.h
+      DEPENDS
+        libc.hdr.stdint_proxy
+        libc.hdr.types.size_t
+        libc.hdr.types.ssize_t
+        libc.hdr.types.struct_timespec
+    )
+  endif()
   if(LIBC_TARGET_ARCHITECTURE_IS_ARM)
-    list(APPEND libc_hermetic_test_support_srcs ArmBaremetalTestUtils.cpp)
+    add_object_library(ArmBaremetalTestUtils
+      SRCS ArmBaremetalTestUtils.cpp
+      DEPENDS .ArmAArch64BaremetalCommonTestUtils)
+    list(APPEND libc_hermetic_test_support_deps .ArmBaremetalTestUtils)
+  elseif(LIBC_TARGET_ARCHITECTURE_IS_AARCH64)
+    add_object_library(AArch64BaremetalTestUtils
+      SRCS AArch64BaremetalTestUtils.cpp
+      DEPENDS .ArmAArch64BaremetalCommonTestUtils)
+    list(APPEND libc_hermetic_test_support_deps .AArch64BaremetalTestUtils)
   elseif(LIBC_TARGET_ARCHITECTURE_IS_HEXAGON)
     list(APPEND libc_hermetic_test_support_srcs HexagonBaremetalTestUtils.cpp)
+    list(APPEND libc_hermetic_test_support_deps
+      libc.hdr.types.size_t
+      libc.hdr.types.ssize_t
+      libc.hdr.types.struct_timespec
+    )
   endif()
-  list(APPEND libc_hermetic_test_support_deps
-    libc.hdr.types.size_t
-    libc.hdr.types.ssize_t
-    libc.hdr.types.struct_timespec
-  )
 endif()
 
 add_unittest_framework_library(

``````````

</details>


https://github.com/llvm/llvm-project/pull/247228


More information about the libc-commits mailing list