[libc-commits] [libc] [libc][uefi] add crt1 (PR #132150)
Tristan Ross via libc-commits
libc-commits at lists.llvm.org
Fri Mar 21 11:02:12 PDT 2025
https://github.com/RossComputerGuy updated https://github.com/llvm/llvm-project/pull/132150
>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 1/3] [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
+}
>From dee779f8e6290ada6d31c9aec02cb636ebaa8cfa Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Thu, 20 Mar 2025 13:37:37 -0700
Subject: [PATCH 2/3] [libc][uefi] drop mingw compat in crt0
---
libc/startup/uefi/crt1.cpp | 36 ------------------------------------
1 file changed, 36 deletions(-)
diff --git a/libc/startup/uefi/crt1.cpp b/libc/startup/uefi/crt1.cpp
index 9da8b8c1e9556..0ebd943cdc527 100644
--- a/libc/startup/uefi/crt1.cpp
+++ b/libc/startup/uefi/crt1.cpp
@@ -12,39 +12,6 @@
#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;
@@ -55,11 +22,8 @@ extern "C" EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
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
}
>From 7428fe0c8e50ae8f8d0fb009db4e23d154c85128 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Fri, 21 Mar 2025 11:01:50 -0700
Subject: [PATCH 3/3] [libc][uefi] update efi symbols in efi main
---
libc/include/Uefi.yaml | 4 ++--
libc/startup/uefi/crt1.cpp | 23 +++++++++++++----------
2 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/libc/include/Uefi.yaml b/libc/include/Uefi.yaml
index 9f38ff3f4a497..0bc62cbe81333 100644
--- a/libc/include/Uefi.yaml
+++ b/libc/include/Uefi.yaml
@@ -10,7 +10,7 @@ types:
enums: []
functions: []
objects:
- - object_name: efi_system_table
+ - object_name: __llvm_libc_efi_system_table
object_type: EFI_SYSTEM_TABLE *
- - object_name: efi_image_handle
+ - object_name: __llvm_libc_efi_image_handle
object_type: EFI_HANDLE
diff --git a/libc/startup/uefi/crt1.cpp b/libc/startup/uefi/crt1.cpp
index 0ebd943cdc527..01d4e701583c0 100644
--- a/libc/startup/uefi/crt1.cpp
+++ b/libc/startup/uefi/crt1.cpp
@@ -1,10 +1,10 @@
-//===-- Implementation of crt for UEFI ----------------------------------===//
+//===-- 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"
@@ -12,18 +12,21 @@
#include "include/llvm-libc-types/EFI_SYSTEM_TABLE.h"
#include "src/__support/macros/config.h"
-EFI_HANDLE efi_image_handle;
-EFI_SYSTEM_TABLE *efi_system_table;
+extern "C" {
+EFI_HANDLE __llvm_libc_efi_image_handle;
+EFI_SYSTEM_TABLE *__llvm_libc_efi_system_table;
-extern "C" int main(int argc, char **argv, char **envp);
+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;
+EFI_STATUS EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
+ __llvm_libc_efi_image_handle = ImageHandle;
+ __llvm_libc_efi_system_table = SystemTable;
- main(0, NULL, NULL);
+ // TODO: we need the EFI_SHELL_PROTOCOL, malloc, free, and UTF16 -> UTF8
+ // conversion.
+ main(0, nullptr, nullptr);
// TODO: convert the return value of main to EFI_STATUS
return 0; // TODO: EFI_SUCCESS
}
+}
More information about the libc-commits
mailing list