Skip to content

Commit b12c594

Browse files
[libc][uefi] add crt1
1 parent d0d33d2 commit b12c594

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

libc/startup/uefi/CMakeLists.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function(add_startup_object name)
2+
cmake_parse_arguments(
3+
"ADD_STARTUP_OBJECT"
4+
"ALIAS" # Option argument
5+
"SRC" # Single value arguments
6+
"DEPENDS;COMPILE_OPTIONS" # Multi value arguments
7+
${ARGN}
8+
)
9+
10+
get_fq_target_name(${name} fq_target_name)
11+
if(ADD_STARTUP_OBJECT_ALIAS)
12+
get_fq_deps_list(fq_dep_list ${ADD_STARTUP_OBJECT_DEPENDS})
13+
add_library(${fq_target_name} ALIAS ${fq_dep_list})
14+
return()
15+
endif()
16+
17+
add_object_library(
18+
${name}
19+
SRCS ${ADD_STARTUP_OBJECT_SRC}
20+
COMPILE_OPTIONS ${ADD_STARTUP_OBJECT_COMPILE_OPTIONS}
21+
${ADD_STARTUP_OBJECT_UNPARSED_ARGUMENTS}
22+
DEPENDS ${ADD_STARTUP_OBJECT_DEPENDS}
23+
)
24+
set_target_properties(
25+
${fq_target_name}
26+
PROPERTIES
27+
OUTPUT_NAME ${name}.o
28+
)
29+
endfunction()
30+
31+
add_startup_object(
32+
crt1
33+
SRCS
34+
crt1.cpp
35+
)
36+
37+
add_custom_target(libc-startup)
38+
set(startup_components crt1)
39+
foreach(target IN LISTS startup_components)
40+
set(fq_target_name libc.startup.uefi.${target})
41+
add_dependencies(libc-startup ${fq_target_name})
42+
install(FILES $<TARGET_OBJECTS:${fq_target_name}>
43+
DESTINATION ${LIBC_INSTALL_LIBRARY_DIR}
44+
RENAME $<TARGET_PROPERTY:${fq_target_name},OUTPUT_NAME>
45+
COMPONENT libc)
46+
endforeach()

libc/startup/uefi/crt1.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===-- Implementation of crt for UEFI ----------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===--------------------------------------------------------------------===//
8+
9+
#include "include/llvm-libc-macros/stdlib-macros.h"
10+
#include "include/llvm-libc-types/EFI_HANDLE.h"
11+
#include "include/llvm-libc-types/EFI_STATUS.h"
12+
#include "include/llvm-libc-types/EFI_SYSTEM_TABLE.h"
13+
#include "src/__support/macros/config.h"
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
17+
using InitCallback = void(void);
18+
using FiniCallback = void(void);
19+
extern "C" InitCallback *__CTOR_LIST__[];
20+
extern "C" FiniCallback *__DTOR_LIST__[];
21+
22+
static void call_init_array_callbacks() {
23+
unsigned long nptrs = (unsigned long)__CTOR_LIST__[0];
24+
unsigned long i;
25+
26+
if (nptrs == ~0ul) {
27+
for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++)
28+
;
29+
}
30+
31+
for (i = nptrs; i >= 1; i--) {
32+
__CTOR_LIST__[i]();
33+
}
34+
}
35+
36+
static void call_fini_array_callbacks() {
37+
unsigned long nptrs = 0;
38+
39+
for (nptrs = 0; __DTOR_LIST__[nptrs + 1] != 0; nptrs++)
40+
;
41+
42+
for (unsigned long i = nptrs; i >= 1; i--) {
43+
__DTOR_LIST__[i]();
44+
}
45+
}
46+
} // namespace LIBC_NAMESPACE_DECL
47+
48+
EFI_HANDLE efi_image_handle;
49+
EFI_SYSTEM_TABLE *efi_system_table;
50+
51+
extern "C" int main(int argc, char **argv, char **envp);
52+
53+
extern "C" EFI_STATUS EfiMain(EFI_HANDLE ImageHandle,
54+
EFI_SYSTEM_TABLE *SystemTable) {
55+
efi_image_handle = ImageHandle;
56+
efi_system_table = SystemTable;
57+
58+
LIBC_NAMESPACE::call_init_array_callbacks();
59+
60+
main(0, NULL, NULL);
61+
62+
LIBC_NAMESPACE::call_fini_array_callbacks();
63+
// TODO: convert the return value of main to EFI_STATUS
64+
return 0; // TODO: EFI_SUCCESS
65+
}

0 commit comments

Comments
 (0)