Skip to content

Commit b3e71ec

Browse files
authored
[libc] Provide __libc_{init,fini}_array for baremetal (llvm#90828)
These are provided by newlib and many baremetal projects assume they're available rather than providing their own implementation.
1 parent f6ff87d commit b3e71ec

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

libc/cmake/modules/LLVMLibCObjectRules.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,6 @@ function(create_entrypoint_object fq_target_name)
246246
if(NOT ADD_ENTRYPOINT_OBJ_SRCS)
247247
message(FATAL_ERROR "`add_entrypoint_object` rule requires SRCS to be specified.")
248248
endif()
249-
if(NOT ADD_ENTRYPOINT_OBJ_HDRS)
250-
message(FATAL_ERROR "`add_entrypoint_object` rule requires HDRS to be specified.")
251-
endif()
252249
if(NOT ADD_ENTRYPOINT_OBJ_CXX_STANDARD)
253250
set(ADD_ENTRYPOINT_OBJ_CXX_STANDARD ${CMAKE_CXX_STANDARD})
254251
endif()

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ set(TARGET_LIBC_ENTRYPOINTS
183183

184184
# time.h entrypoints
185185
libc.src.time.difftime
186+
187+
# internal entrypoints
188+
libc.startup.baremetal.init
189+
libc.startup.baremetal.fini
186190
)
187191

188192
set(TARGET_LIBM_ENTRYPOINTS

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ set(TARGET_LIBC_ENTRYPOINTS
183183

184184
# time.h entrypoints
185185
libc.src.time.difftime
186+
187+
# internal entrypoints
188+
libc.startup.baremetal.init
189+
libc.startup.baremetal.fini
186190
)
187191

188192
set(TARGET_LIBM_ENTRYPOINTS

libc/startup/baremetal/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_entrypoint_object(
2+
init
3+
SRCS
4+
init.cpp
5+
)
6+
7+
add_entrypoint_object(
8+
fini
9+
SRCS
10+
fini.cpp
11+
)

libc/startup/baremetal/fini.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===-- Implementation file of __libc_fini_array --------------------------===//
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 <stddef.h>
10+
#include <stdint.h>
11+
12+
extern "C" {
13+
extern uintptr_t __fini_array_start[];
14+
extern uintptr_t __fini_array_end[];
15+
}
16+
17+
namespace LIBC_NAMESPACE {
18+
19+
using FiniCallback = void(void);
20+
21+
extern "C" void __libc_fini_array(void) {
22+
size_t fini_array_size = __fini_array_end - __fini_array_start;
23+
for (size_t i = fini_array_size; i > 0; --i)
24+
reinterpret_cast<FiniCallback *>(__fini_array_start[i - 1])();
25+
}
26+
27+
} // namespace LIBC_NAMESPACE

libc/startup/baremetal/init.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- Implementation file of __libc_init_array --------------------------===//
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 <stddef.h>
10+
#include <stdint.h>
11+
12+
extern "C" {
13+
extern uintptr_t __preinit_array_start[];
14+
extern uintptr_t __preinit_array_end[];
15+
extern uintptr_t __init_array_start[];
16+
extern uintptr_t __init_array_end[];
17+
}
18+
19+
namespace LIBC_NAMESPACE {
20+
21+
using InitCallback = void(void);
22+
23+
extern "C" void __libc_init_array(void) {
24+
size_t preinit_array_size = __preinit_array_end - __preinit_array_start;
25+
for (size_t i = 0; i < preinit_array_size; ++i)
26+
reinterpret_cast<InitCallback *>(__preinit_array_start[i])();
27+
size_t init_array_size = __init_array_end - __init_array_start;
28+
for (size_t i = 0; i < init_array_size; ++i)
29+
reinterpret_cast<InitCallback *>(__init_array_start[i])();
30+
}
31+
32+
} // namespace LIBC_NAMESPACE

0 commit comments

Comments
 (0)