-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[libc][uefi] add crt1 #132150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[libc][uefi] add crt1 #132150
Changes from 8 commits
b12c594
dee779f
7428fe0
bf566bb
40a26a0
e5a4692
d963ce2
4ccc169
65da577
553ec1b
6648c0c
0596ada
f87345a
0a47352
ae46dfc
5e4c64f
74dd600
3aaccf0
b5dbce3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| //===---------- UEFI implementation of error utils ------------*- C++ -*-===// | ||
|
||
| // | ||
| // 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 | ||
| // | ||
| //===-------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_ERROR_H | ||
| #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_ERROR_H | ||
|
|
||
| #include "errno.h" | ||
| #include "include/llvm-libc-types/EFI_STATUS.h" | ||
| #include "limits.h" | ||
| #include "src/__support/macros/config.h" | ||
|
||
|
|
||
| namespace LIBC_NAMESPACE_DECL { | ||
|
|
||
| #define EFI_ERROR_MAX_BIT (1 << (sizeof(EFI_STATUS) * sizeof(char) - 1)) | ||
| #define EFI_ENCODE_ERROR(value) \ | ||
| (EFI_ERROR_MAX_BIT | (EFI_ERROR_MAX_BIT >> 2) | (value)) | ||
| #define EFI_ENCODE_WARNING(value) ((EFI_ERROR_MAX_BIT >> 2) | (value)) | ||
|
|
||
| static constexpr struct errno_efi_status_entry { | ||
| EFI_STATUS status; | ||
| int errno_value; | ||
| } uefi_status_errno_map[] = { | ||
| {EFI_SUCCESS, 0}, | ||
| {EFI_ENCODE_ERROR(EFI_LOAD_ERROR), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_INVALID_PARAMETER), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_BAD_BUFFER_SIZE), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_NOT_READY), EBUSY}, | ||
| {EFI_ENCODE_ERROR(EFI_DEVICE_ERROR), EIO}, | ||
| {EFI_ENCODE_ERROR(EFI_WRITE_PROTECTED), EPERM}, | ||
| {EFI_ENCODE_ERROR(EFI_OUT_OF_RESOURCES), ENOMEM}, | ||
| {EFI_ENCODE_ERROR(EFI_VOLUME_CORRUPTED), EROFS}, | ||
| {EFI_ENCODE_ERROR(EFI_VOLUME_FULL), ENOSPC}, | ||
| {EFI_ENCODE_ERROR(EFI_NO_MEDIA), ENODEV}, | ||
| {EFI_ENCODE_ERROR(EFI_MEDIA_CHANGED), ENXIO}, | ||
| {EFI_ENCODE_ERROR(EFI_NOT_FOUND), ENOENT}, | ||
| {EFI_ENCODE_ERROR(EFI_ACCESS_DENIED), EACCES}, | ||
| {EFI_ENCODE_ERROR(EFI_NO_RESPONSE), EBUSY}, | ||
| {EFI_ENCODE_ERROR(EFI_NO_MAPPING), ENODEV}, | ||
| {EFI_ENCODE_ERROR(EFI_TIMEOUT), EBUSY}, | ||
| {EFI_ENCODE_ERROR(EFI_NOT_STARTED), EAGAIN}, | ||
| {EFI_ENCODE_ERROR(EFI_ALREADY_STARTED), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_ABORTED), EFAULT}, | ||
| {EFI_ENCODE_ERROR(EFI_ICMP_ERROR), EIO}, | ||
| {EFI_ENCODE_ERROR(EFI_TFTP_ERROR), EIO}, | ||
| {EFI_ENCODE_ERROR(EFI_PROTOCOL_ERROR), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_INCOMPATIBLE_VERSION), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_SECURITY_VIOLATION), EPERM}, | ||
| {EFI_ENCODE_ERROR(EFI_CRC_ERROR), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_END_OF_MEDIA), EPIPE}, | ||
| {EFI_ENCODE_ERROR(EFI_END_OF_FILE), EPIPE}, | ||
| {EFI_ENCODE_ERROR(EFI_INVALID_LANGUAGE), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_COMPROMISED_DATA), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_IP_ADDRESS_CONFLICT), EINVAL}, | ||
| {EFI_ENCODE_ERROR(EFI_HTTP_ERROR), EIO}, | ||
| {EFI_ENCODE_WARNING(EFI_WARN_UNKNOWN_GLYPH), EINVAL}, | ||
| {EFI_ENCODE_WARNING(EFI_WARN_DELETE_FAILURE), EROFS}, | ||
| {EFI_ENCODE_WARNING(EFI_WARN_WRITE_FAILURE), EROFS}, | ||
| {EFI_ENCODE_WARNING(EFI_WARN_BUFFER_TOO_SMALL), E2BIG}, | ||
| {EFI_ENCODE_WARNING(EFI_WARN_STALE_DATA), EINVAL}, | ||
| {EFI_ENCODE_WARNING(EFI_WARN_FILE_SYSTEM), EROFS}, | ||
| {EFI_ENCODE_WARNING(EFI_WARN_RESET_REQUIRED), EINTR}, | ||
| }; | ||
|
||
|
|
||
| static constexpr size_t uefi_status_errno_map_length = | ||
|
||
| sizeof(uefi_status_errno_map) / sizeof(uefi_status_errno_map[0]); | ||
|
|
||
| static inline int uefi_status_to_errno(EFI_STATUS status) { | ||
|
||
| for (size_t i = 0; i < uefi_status_errno_map_length; i++) { | ||
| const struct errno_efi_status_entry *entry = &uefi_status_errno_map[i]; | ||
| if (entry->status == status) | ||
| return entry->errno_value; | ||
| } | ||
|
|
||
| // Unknown type | ||
| __builtin_unreachable(); | ||
|
||
| } | ||
|
|
||
| static inline EFI_STATUS errno_to_uefi_status(int errno_value) { | ||
| for (size_t i = 0; i < uefi_status_errno_map_length; i++) { | ||
| const struct errno_efi_status_entry *entry = &uefi_status_errno_map[i]; | ||
| if (entry->errno_value == errno_value) | ||
| return entry->status; | ||
| } | ||
|
|
||
| // Unknown type | ||
| __builtin_unreachable(); | ||
| } | ||
|
|
||
| } // namespace LIBC_NAMESPACE_DECL | ||
|
|
||
| #endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_UEFI_ERROR_H | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -7,14 +7,15 @@ | |||
| //===-----------------------------------------------------------------===// | ||||
|
|
||||
| #include "src/__support/OSUtil/exit.h" | ||||
| #include "include/Uefi.h" | ||||
| #include "Uefi.h" | ||||
|
||||
| strtointeger(const char *__restrict src, int base, |
Here and elsewhere in this patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I just needed the EFI_SYSTEM_TABLE's stuff.
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,48 @@ | ||||||||||||||
| function(add_startup_object name) | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I apparently missed that each of the targets currently has their own definition of this. Please add a todo referencing #133156 (the bug I've filed for this)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||
| 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 | ||||||||||||||
| DEPENDS | ||||||||||||||
| libc.src.__support.OSUtil.uefi.uefi_util | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To have an object file depend on object files (or a library), you'd need to use relocatable linking, see https://maskray.me/blog/2022-11-21-relocatable-linking. We use this approach for Linux llvm-project/libc/startup/linux/CMakeLists.txt Lines 114 to 119 in 3840f78
Unfortunately, as far as I'm aware COFF linkers don't support relocatable linking so that approach isn't going to work. The most straightforward alternative is probably to move
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha, yeah I think for the purposes of this platform and the capabilities currently in |
||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| 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() | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| //===-- Implementation of crt for UEFI ------------------------------------===// | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also needs a test, see https://github.com/llvm/llvm-project/blob/main/libc/test/integration/startup/linux/main_without_args.cpp for an example
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| // | ||
| // 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/OSUtil/uefi/error.h" | ||
| #include "src/__support/macros/config.h" | ||
|
|
||
| extern "C" { | ||
| EFI_HANDLE __llvm_libc_efi_image_handle; | ||
| EFI_SYSTEM_TABLE *__llvm_libc_efi_system_table; | ||
|
|
||
| int main(int argc, char **argv, char **envp); | ||
|
|
||
| EFI_STATUS EfiMain(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) { | ||
| __llvm_libc_efi_image_handle = ImageHandle; | ||
| __llvm_libc_efi_system_table = SystemTable; | ||
|
|
||
| // TODO: we need the EFI_SHELL_PROTOCOL, malloc, free, and UTF16 -> UTF8 | ||
| // conversion. | ||
| return LIBC_NAMESPACE::errno_to_uefi_status(main(0, nullptr, nullptr)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you're removing the usage of
Uefi.h.defyou should also remove the file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember this change... Not sure why the line was removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh now I remember, removed the file now.