[libc] Provide __libc_{init,fini}_array for baremetal (#90828)

These are provided by newlib and many baremetal projects assume they're
available rather than providing their own implementation.
This commit is contained in:
Petr Hosek 2024-05-21 15:46:04 -07:00 committed by GitHub
parent f6ff87dd90
commit b3e71ec9f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 78 additions and 3 deletions

View File

@ -246,9 +246,6 @@ function(create_entrypoint_object fq_target_name)
if(NOT ADD_ENTRYPOINT_OBJ_SRCS)
message(FATAL_ERROR "`add_entrypoint_object` rule requires SRCS to be specified.")
endif()
if(NOT ADD_ENTRYPOINT_OBJ_HDRS)
message(FATAL_ERROR "`add_entrypoint_object` rule requires HDRS to be specified.")
endif()
if(NOT ADD_ENTRYPOINT_OBJ_CXX_STANDARD)
set(ADD_ENTRYPOINT_OBJ_CXX_STANDARD ${CMAKE_CXX_STANDARD})
endif()

View File

@ -183,6 +183,10 @@ set(TARGET_LIBC_ENTRYPOINTS
# time.h entrypoints
libc.src.time.difftime
# internal entrypoints
libc.startup.baremetal.init
libc.startup.baremetal.fini
)
set(TARGET_LIBM_ENTRYPOINTS

View File

@ -183,6 +183,10 @@ set(TARGET_LIBC_ENTRYPOINTS
# time.h entrypoints
libc.src.time.difftime
# internal entrypoints
libc.startup.baremetal.init
libc.startup.baremetal.fini
)
set(TARGET_LIBM_ENTRYPOINTS

View File

@ -0,0 +1,11 @@
add_entrypoint_object(
init
SRCS
init.cpp
)
add_entrypoint_object(
fini
SRCS
fini.cpp
)

View File

@ -0,0 +1,27 @@
//===-- Implementation file of __libc_fini_array --------------------------===//
//
// 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 <stddef.h>
#include <stdint.h>
extern "C" {
extern uintptr_t __fini_array_start[];
extern uintptr_t __fini_array_end[];
}
namespace LIBC_NAMESPACE {
using FiniCallback = void(void);
extern "C" void __libc_fini_array(void) {
size_t fini_array_size = __fini_array_end - __fini_array_start;
for (size_t i = fini_array_size; i > 0; --i)
reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
}
} // namespace LIBC_NAMESPACE

View File

@ -0,0 +1,32 @@
//===-- Implementation file of __libc_init_array --------------------------===//
//
// 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 <stddef.h>
#include <stdint.h>
extern "C" {
extern uintptr_t __preinit_array_start[];
extern uintptr_t __preinit_array_end[];
extern uintptr_t __init_array_start[];
extern uintptr_t __init_array_end[];
}
namespace LIBC_NAMESPACE {
using InitCallback = void(void);
extern "C" void __libc_init_array(void) {
size_t preinit_array_size = __preinit_array_end - __preinit_array_start;
for (size_t i = 0; i < preinit_array_size; ++i)
reinterpret_cast<InitCallback *>(__preinit_array_start[i])();
size_t init_array_size = __init_array_end - __init_array_start;
for (size_t i = 0; i < init_array_size; ++i)
reinterpret_cast<InitCallback *>(__init_array_start[i])();
}
} // namespace LIBC_NAMESPACE