Skip to content

Commit 16124a3

Browse files
authored
[libc] __stack_chk_fail baremetal dependencies (#76412)
`__stack_chk_fail` uses `write_to_stderr` and `abort` but these currently aren't included in baremetal targets resulting in a CMake build error: ``` CMake Error at /llvm-project/libc/cmake/modules/LLVMLibCObjectRules.cmake:693 (target_link_libraries): Target "libc.src.stdlib.abort" of type UTILITY may not be linked into another target. One may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to executables with the ENABLE_EXPORTS property set. Call Stack (most recent call first): /llvm-project/libc/cmake/modules/LLVMLibCObjectRules.cmake:811 (create_entrypoint_object) /llvm-project/libc/cmake/modules/LLVMLibCObjectRules.cmake:891 (expand_flags_for_entrypoint_object) /llvm-project/libc/src/compiler/generic/CMakeLists.txt:1 (add_entrypoint_object) CMake Error at /llvm-project/libc/cmake/modules/LLVMLibCLibraryRules.cmake:5 (get_target_property): get_target_property() called with non-existent target "libc.src.__support.OSUtil.osutil". Call Stack (most recent call first): /llvm-project/libc/cmake/modules/LLVMLibCLibraryRules.cmake:36 (collect_object_file_deps) /llvm-project/libc/cmake/modules/LLVMLibCLibraryRules.cmake:36 (collect_object_file_deps) /llvm-project/libc/cmake/modules/LLVMLibCLibraryRules.cmake:85 (collect_object_file_deps) /llvm-project/libc/lib/CMakeLists.txt:26 (add_entrypoint_library) ``` To address these errors, we need to include `abort` in the list of baremetal entrypoints. We also need to provide `write_to_stderr` baremetal implementation, but this is challenging since there is no uniform way to print a message on these platforms (sometimes there may not be any way to print a message). We instead defer to `__libc_log_write` which can be provided by the underlying platform. We use a similar approach for `quick_exit`, defering to `__libc_quick_exit`.
1 parent 4e347b4 commit 16124a3

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

libc/config/baremetal/arm/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ set(TARGET_LIBC_ENTRYPOINTS
7474
libc.src.stdio.vsnprintf
7575

7676
# stdlib.h entrypoints
77+
libc.src.stdlib.abort
7778
libc.src.stdlib.abs
7879
libc.src.stdlib.atoi
7980
libc.src.stdlib.atof

libc/config/baremetal/riscv/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ set(TARGET_LIBC_ENTRYPOINTS
7474
libc.src.stdio.vsnprintf
7575

7676
# stdlib.h entrypoints
77+
libc.src.stdlib.abort
7778
libc.src.stdlib.abs
7879
libc.src.stdlib.atoi
7980
libc.src.stdlib.atol
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_header_library(
2+
baremetal_util
3+
HDRS
4+
io.h
5+
quick_exit.h
6+
DEPENDS
7+
libc.src.__support.common
8+
libc.src.__support.CPP.string_view
9+
)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===---------- Baremetal 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_BAREMETAL_IO_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
11+
12+
#include "src/__support/CPP/string_view.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
// This is intended to be provided by the vendor.
17+
extern "C" void __llvm_libc_log_write(const char* msg, size_t len);
18+
19+
void write_to_stderr(cpp::string_view msg) {
20+
__llvm_libc_log_write(msg.data(), msg.size());
21+
}
22+
23+
} // namespace LIBC_NAMESPACE
24+
25+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_IO_H
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===----- Baremetal implementation of a quick 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+
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_QUICK_EXIT_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_QUICK_EXIT_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
// This is intended to be provided by the vendor.
15+
extern "C" void __llvm_libc_quick_exit(int status);
16+
17+
void quick_exit(int status) {
18+
__llvm_libc_quick_exit(status);
19+
}
20+
21+
} // namespace LIBC_NAMESPACE
22+
23+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_BAREMETAL_QUICK_EXIT_H

libc/src/__support/OSUtil/io.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#include "linux/io.h"
2020
#elif defined(__Fuchsia__)
2121
#include "fuchsia/io.h"
22+
#elif defined(__ELF__)
23+
// TODO: Ideally we would have LIBC_TARGET_OS_IS_BAREMETAL.
24+
#include "baremetal/io.h"
2225
#endif
2326

2427
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_IO_H

libc/src/__support/OSUtil/quick_exit.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#include "darwin/quick_exit.h"
1818
#elif defined(__linux__)
1919
#include "linux/quick_exit.h"
20+
#elif defined(__ELF__)
21+
// TODO: Ideally we would have LIBC_TARGET_OS_IS_BAREMETAL.
22+
#include "baremetal/quick_exit.h"
2023
#endif
2124

2225
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_QUICK_EXIT_H

0 commit comments

Comments
 (0)