[libc-commits] [libc] [libc][uefi] add crt1 (PR #132150)

Tristan Ross via libc-commits libc-commits at lists.llvm.org
Wed Mar 19 22:52:34 PDT 2025


https://github.com/RossComputerGuy created https://github.com/llvm/llvm-project/pull/132150

Adds `crt1.o` for the UEFI platform in the LLVM C library. This makes things start to become useful.

>From b12c594943843f11a785a4a51a8fd2a15529db3b Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Wed, 19 Mar 2025 22:51:20 -0700
Subject: [PATCH] [libc][uefi] add crt1

---
 libc/startup/uefi/CMakeLists.txt | 46 ++++++++++++++++++++++
 libc/startup/uefi/crt1.cpp       | 65 ++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+)
 create mode 100644 libc/startup/uefi/CMakeLists.txt
 create mode 100644 libc/startup/uefi/crt1.cpp

diff --git a/libc/startup/uefi/CMakeLists.txt b/libc/startup/uefi/CMakeLists.txt
new file mode 100644
index 0000000000000..741081144001c
--- /dev/null
+++ b/libc/startup/uefi/CMakeLists.txt
@@ -0,0 +1,46 @@
+function(add_startup_object name)
+  cmake_parse_arguments(
+    "ADD_STARTUP_OBJECT"
+    "ALIAS" # Option argument
+    "SRC"   # Single value arguments
+    "DEPENDS;COMPILE_OPTIONS" # Multi value arguments
+    ${ARGN}
+  )
+
+  get_fq_target_name(${name} fq_target_name)
+  if(ADD_STARTUP_OBJECT_ALIAS)
+    get_fq_deps_list(fq_dep_list ${ADD_STARTUP_OBJECT_DEPENDS})
+    add_library(${fq_target_name} ALIAS ${fq_dep_list})
+    return()
+  endif()
+
+  add_object_library(
+    ${name}
+    SRCS ${ADD_STARTUP_OBJECT_SRC}
+    COMPILE_OPTIONS ${ADD_STARTUP_OBJECT_COMPILE_OPTIONS}
+    ${ADD_STARTUP_OBJECT_UNPARSED_ARGUMENTS}
+    DEPENDS ${ADD_STARTUP_OBJECT_DEPENDS}
+  )
+  set_target_properties(
+    ${fq_target_name}
+    PROPERTIES
+      OUTPUT_NAME ${name}.o
+  )
+endfunction()
+
+add_startup_object(
+  crt1
+  SRCS
+  crt1.cpp
+)
+
+add_custom_target(libc-startup)
+set(startup_components crt1)
+foreach(target IN LISTS startup_components)
+  set(fq_target_name libc.startup.uefi.${target})
+  add_dependencies(libc-startup ${fq_target_name})
+  install(FILES $<TARGET_OBJECTS:${fq_target_name}>
+          DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
+          RENAME $<TARGET_PROPERTY:${fq_target_name},OUTPUT_NAME>
+          COMPONENT libc)
+endforeach()
diff --git a/libc/startup/uefi/crt1.cpp b/libc/startup/uefi/crt1.cpp
new file mode 100644
index 0000000000000..9da8b8c1e9556
--- /dev/null
+++ b/libc/startup/uefi/crt1.cpp
@@ -0,0 +1,65 @@
+//===-- Implementation of crt for UEFI ----------------------------------===//
+//
+// 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 "include/llvm-libc-macros/stdlib-macros.h"
+#include "include/llvm-libc-types/EFI_HANDLE.h"
+#include "include/llvm-libc-types/EFI_STATUS.h"
+#include "include/llvm-libc-types/EFI_SYSTEM_TABLE.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+using InitCallback = void(void);
+using FiniCallback = void(void);
+extern "C" InitCallback *__CTOR_LIST__[];
+extern "C" FiniCallback *__DTOR_LIST__[];
+
+static void call_init_array_callbacks() {
+  unsigned long nptrs = (unsigned long)__CTOR_LIST__[0];
+  unsigned long i;
+
+  if (nptrs == ~0ul) {
+    for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
+      ;
+  }
+
+  for (i = nptrs; i >= 1; i--) {
+    __CTOR_LIST__[i]();
+  }
+}
+
+static void call_fini_array_callbacks() {
+  unsigned long nptrs = 0;
+
+  for (nptrs = 0; __DTOR_LIST__[nptrs + 1] != 0; nptrs++)
+    ;
+
+  for (unsigned long i = nptrs; i >= 1; i--) {
+    __DTOR_LIST__[i]();
+  }
+}
+} // namespace LIBC_NAMESPACE_DECL
+
+EFI_HANDLE efi_image_handle;
+EFI_SYSTEM_TABLE *efi_system_table;
+
+extern "C" int main(int argc, char **argv, char **envp);
+
+extern "C" EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
+                              EFI_SYSTEM_TABLE *SystemTable) {
+  efi_image_handle = ImageHandle;
+  efi_system_table = SystemTable;
+
+  LIBC_NAMESPACE::call_init_array_callbacks();
+
+  main(0, NULL, NULL);
+
+  LIBC_NAMESPACE::call_fini_array_callbacks();
+  // TODO: convert the return value of main to EFI_STATUS
+  return 0; // TODO: EFI_SUCCESS
+}



More information about the libc-commits mailing list