[libc-commits] [libc] [libc] Minimal crt1 for baremetal RISC-V (PR #224714)
via libc-commits
libc-commits at lists.llvm.org
Fri Sep 18 12:28:36 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Petr Hosek (petrhosek)
<details>
<summary>Changes</summary>
This is a minimal implementation of crt1 for baremetal RISC-V similar to the Arm version.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/224714.diff
2 Files Affected:
- (added) libc/startup/baremetal/riscv/CMakeLists.txt (+16)
- (added) libc/startup/baremetal/riscv/start.cpp (+88)
``````````diff
diff --git a/libc/startup/baremetal/riscv/CMakeLists.txt b/libc/startup/baremetal/riscv/CMakeLists.txt
new file mode 100644
index 0000000000000..5a5a6b363c1cc
--- /dev/null
+++ b/libc/startup/baremetal/riscv/CMakeLists.txt
@@ -0,0 +1,16 @@
+add_startup_object(
+ crt1
+ SRC
+ start.cpp
+ DEPENDS
+ libc.src.stdlib.atexit
+ libc.src.stdlib.exit
+ libc.src.string.memcpy
+ libc.src.string.memset
+ libc.startup.baremetal.init
+ libc.startup.baremetal.fini
+ COMPILE_OPTIONS
+ -ffreestanding # To avoid compiler warnings about calling the main function.
+ -fno-builtin
+ -Wno-global-constructors
+)
diff --git a/libc/startup/baremetal/riscv/start.cpp b/libc/startup/baremetal/riscv/start.cpp
new file mode 100644
index 0000000000000..6cc05c0de36a8
--- /dev/null
+++ b/libc/startup/baremetal/riscv/start.cpp
@@ -0,0 +1,88 @@
+//===-- Implementation of crt for riscv -----------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/stdint_proxy.h"
+#include "src/__support/macros/config.h"
+#include "src/stdlib/atexit.h"
+#include "src/stdlib/exit.h"
+#include "src/string/memcpy.h"
+#include "src/string/memset.h"
+#include "startup/baremetal/fini.h"
+#include "startup/baremetal/init.h"
+
+extern "C" {
+int main(int argc, char **argv);
+void _start();
+
+// Semihosting library initialisation if applicable. Required for printf, etc.
+[[gnu::weak]] void _platform_init() {}
+
+// These symbols are provided by the linker. The exact names are not defined by
+// a standard.
+extern uintptr_t __stack;
+extern uintptr_t __data_source[];
+extern uintptr_t __data_start[];
+extern uintptr_t __data_size[];
+extern uintptr_t __bss_start[];
+extern uintptr_t __bss_size[];
+} // extern "C"
+
+namespace {
+[[gnu::aligned(4)]] void trap_handler() {
+ LIBC_NAMESPACE::exit(1);
+}
+} // namespace
+
+namespace LIBC_NAMESPACE_DECL {
+
+[[noreturn]] void do_start() {
+ // Set up trap handling.
+ __asm__ volatile("csrw mtvec, %0" :: "r"(&trap_handler));
+
+#ifdef __riscv_flen
+ // Enable FPU by setting FS bits to Initial (1) in mstatus.
+ __asm__ volatile("csrs mstatus, %0" :: "r"(1UL << 13) : "memory");
+ // Clear fcsr.
+ __asm__ volatile("fscsr zero");
+#endif
+#ifdef __riscv_vector
+ // Enable Vector unit by setting VS bits to Initial (1) in mstatus.
+ __asm__ volatile("csrs mstatus, %0" :: "r"(1UL << 9) : "memory");
+#endif
+
+ LIBC_NAMESPACE::memcpy(__data_start, __data_source,
+ reinterpret_cast<uintptr_t>(__data_size));
+ LIBC_NAMESPACE::memset(__bss_start, '\0',
+ reinterpret_cast<uintptr_t>(__bss_size));
+ __libc_init_array();
+
+ _platform_init();
+ LIBC_NAMESPACE::atexit(&__libc_fini_array);
+ LIBC_NAMESPACE::exit(main(0, nullptr));
+}
+} // namespace LIBC_NAMESPACE_DECL
+
+extern "C" {
+[[gnu::section(".text.init.enter"), gnu::naked]]
+void _start() {
+ // Initialize global pointer if defined by linker.
+ // We use .option norelax to prevent the assembler from relaxing the lla instruction.
+ __asm__ volatile(
+ ".option push\n"
+ ".option norelax\n"
+ "lla gp, __global_pointer$\n"
+ ".option pop\n"
+ );
+
+ // Initialize stack pointer.
+ __asm__ volatile("lla sp, %0" : : "i"(&__stack));
+
+ // Call do_start.
+ __asm__ volatile("tail %0" : : "i"(&LIBC_NAMESPACE::do_start));
+}
+} // extern "C"
``````````
</details>
https://github.com/llvm/llvm-project/pull/224714
More information about the libc-commits
mailing list