Skip to content

Commit

Permalink
[libc] Provide __libc_{init,fini}_array for baremetal (llvm#90828)
Browse files Browse the repository at this point in the history
These are provided by newlib and many baremetal projects assume they're
available rather than providing their own implementation.
  • Loading branch information
petrhosek authored May 21, 2024
1 parent f6ff87d commit b3e71ec
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 3 deletions.
3 changes: 0 additions & 3 deletions libc/cmake/modules/LLVMLibCObjectRules.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions libc/config/baremetal/arm/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions libc/config/baremetal/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions libc/startup/baremetal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
add_entrypoint_object(
init
SRCS
init.cpp
)

add_entrypoint_object(
fini
SRCS
fini.cpp
)
27 changes: 27 additions & 0 deletions libc/startup/baremetal/fini.cpp
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions libc/startup/baremetal/init.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b3e71ec

Please sign in to comment.