Skip to content

Commit bb69499

Browse files
[libc] init uefi (#131246)
Initial UEFI OS target support after the headers. This just defines enough that stuff might try and compile. Test with: ``` $ cmake -S llvm -B build -G Ninja -DLLVM_RUNTIME_TARGETS=x86_64-unknown-uefi-llvm -DRUNTIMES_x86_64-unknown-uefi-llvm_LLVM_ENABLE_RUNTIMES=libc -DRUNTIMES_x86_64-unknown-uefi-llvm_LLVM_LIBC_FULL_BUILD=true -DCMAKE_C_COMPILER_WORKS=true -DCMAKE_CXX_COMPILER_WORKS=true -DLLVM_ENABLE_PROJECTS="clang;lld" -DCMAKE_BUILD_TYPE=Debug -DLLVM_ENABLE_LIBCXX=true -DLLVM_HOST_TRIPLE=aarch64-unknown-linux-gnu -DLLVM_DEFAULT_TARGET_TRIPLE=x86_64-unknown-uefi-llvm -DCMAKE_INSTALL_LIBDIR=build/target/lib $ ninja -C build ```
1 parent e86081b commit bb69499

File tree

11 files changed

+123
-1
lines changed

11 files changed

+123
-1
lines changed

libc/cmake/modules/LLVMLibCArchitectures.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ elseif(LIBC_TARGET_OS STREQUAL "windows")
191191
set(LIBC_TARGET_OS_IS_WINDOWS TRUE)
192192
elseif(LIBC_TARGET_OS STREQUAL "gpu")
193193
set(LIBC_TARGET_OS_IS_GPU TRUE)
194+
elseif(LIBC_TARGET_OS STREQUAL "uefi")
195+
set(LIBC_TARGET_OS_IS_UEFI TRUE)
194196
else()
195197
message(FATAL_ERROR
196198
"Unsupported libc target operating system ${LIBC_TARGET_OS}")

libc/config/uefi/config.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"errno": {
3+
"LIBC_CONF_ERRNO_MODE": {
4+
"value": "LIBC_ERRNO_MODE_SHARED"
5+
}
6+
}
7+
}

libc/config/uefi/entrypoints.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set(TARGET_LIBC_ENTRYPOINTS
2+
# errno.h entrypoints
3+
libc.src.errno.errno
4+
)
5+
6+
set(TARGET_LIBM_ENTRYPOINTS)
7+
8+
set(TARGET_LLVMLIBC_ENTRYPOINTS
9+
${TARGET_LIBC_ENTRYPOINTS}
10+
${TARGET_LIBM_ENTRYPOINTS}
11+
)

libc/config/uefi/headers.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
set(TARGET_PUBLIC_HEADERS
2+
libc.include.errno
3+
libc.include.uefi
4+
)

libc/include/Uefi.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
header: Uefi.h
2-
standards: UEFI
2+
standards:
3+
- uefi
34
macros: []
45
types:
56
- type_name: EFI_BOOT_SERVICES

libc/src/__support/OSUtil/io.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#elif defined(__ELF__)
2525
// TODO: Ideally we would have LIBC_TARGET_OS_IS_BAREMETAL.
2626
#include "baremetal/io.h"
27+
#elif defined(__UEFI__)
28+
#include "uefi/io.h"
2729
#endif
2830

2931
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_IO_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_object_library(
2+
uefi_util
3+
SRCS
4+
io.cpp
5+
exit.cpp
6+
HDRS
7+
io.h
8+
DEPENDS
9+
libc.src.__support.common
10+
libc.src.__support.CPP.string_view
11+
)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-------- UEFI implementation of an exit function ------*- C++ -*-===//
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 "src/__support/OSUtil/exit.h"
10+
#include "include/Uefi.h"
11+
#include "src/__support/macros/config.h"
12+
13+
namespace LIBC_NAMESPACE_DECL {
14+
namespace internal {
15+
16+
[[noreturn]] void exit(int status) {
17+
efi_system_table->BootServices->Exit(efi_image_handle, status, 0, nullptr);
18+
__builtin_unreachable();
19+
}
20+
21+
} // namespace internal
22+
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/OSUtil/uefi/io.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===---------- UEFI implementation of IO utils ------------*- C++ -*-===//
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 "io.h"
10+
11+
#include "src/__support/CPP/string_view.h"
12+
#include "src/__support/macros/config.h"
13+
14+
namespace LIBC_NAMESPACE_DECL {
15+
16+
ssize_t read_from_stdin(char *buf, size_t size) { return 0; }
17+
18+
void write_to_stdout(cpp::string_view msg) {
19+
// TODO: use mbstowcs once implemented
20+
for (size_t i = 0; i < msg.size(); i++) {
21+
char16_t e[2] = {msg[i], 0};
22+
efi_system_table->ConOut->OutputString(
23+
efi_system_table->ConOut, reinterpret_cast<const char16_t *>(&e));
24+
}
25+
}
26+
27+
void write_to_stderr(cpp::string_view msg) {
28+
// TODO: use mbstowcs once implemented
29+
for (size_t i = 0; i < msg.size(); i++) {
30+
char16_t e[2] = {msg[i], 0};
31+
efi_system_table->StdErr->OutputString(
32+
efi_system_table->StdErr, reinterpret_cast<const char16_t *>(&e));
33+
}
34+
}
35+
36+
} // namespace LIBC_NAMESPACE_DECL

libc/src/__support/OSUtil/uefi/io.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===---------- UEFI implementation of IO utils ------------*- C++ -*-===//
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+
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_IO_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_IO_H
11+
12+
#include "include/llvm-libc-types/size_t.h"
13+
#include "include/llvm-libc-types/ssize_t.h"
14+
#include "src/__support/CPP/string_view.h"
15+
#include "src/__support/macros/config.h"
16+
17+
namespace LIBC_NAMESPACE_DECL {
18+
19+
ssize_t read_from_stdin(char *buf, size_t size);
20+
void write_to_stderr(cpp::string_view msg);
21+
void write_to_stdout(cpp::string_view msg);
22+
23+
} // namespace LIBC_NAMESPACE_DECL
24+
25+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_IO_H

libc/utils/hdrgen/hdrgen/header.py

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"bsd": "BSD",
4444
"gnu": "GNU",
4545
"linux": "Linux",
46+
"uefi": "UEFI",
4647
}
4748

4849
HEADER_TEMPLATE = """\

0 commit comments

Comments
 (0)