diff --git a/libc/config/linux/CMakeLists.txt b/libc/config/linux/CMakeLists.txt --- a/libc/config/linux/CMakeLists.txt +++ b/libc/config/linux/CMakeLists.txt @@ -10,4 +10,15 @@ libc.src.__support.common ) +add_object_library( + lowlevel_utils + SRCS + ${LIBC_TARGET_MACHINE}/machine_lowlevel.cpp + platform_lowlevel.cpp + COMPILE_OPTIONS + # Some of the implementations assume presence of the frame + # pointer. + -fno-omit-frame-pointer +) + add_subdirectory(x86_64) diff --git a/libc/config/linux/platform_lowlevel.h.inc b/libc/config/linux/platform_lowlevel.h.inc new file mode 100644 --- /dev/null +++ b/libc/config/linux/platform_lowlevel.h.inc @@ -0,0 +1,19 @@ +//===-- Lowlevel Linux utilities --------------------------------*- 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 +// +//===----------------------------------------------------------------------===// + +%%begin() + +#include + +namespace __llvm_libc { + +static __attribute__((always_inline)) uintptr_t get_current_bp() { + return reinterpret_cast(__builtin_frame_address(0)); +} + +} // namespace llvm_libc diff --git a/libc/config/linux/platform_lowlevel.cpp b/libc/config/linux/platform_lowlevel.cpp new file mode 100644 --- /dev/null +++ b/libc/config/linux/platform_lowlevel.cpp @@ -0,0 +1,18 @@ +//===-- Implementation of machine independent low level utils -------------===// +// +// 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 + +namespace __llvm_libc { + +uintptr_t get_current_pc() { + return reinterpret_cast( + __builtin_extract_return_addr(__builtin_return_address(0))); +} + +} // namespace __llvm_libc diff --git a/libc/config/linux/x86_64/machine_lowlevel.cpp b/libc/config/linux/x86_64/machine_lowlevel.cpp new file mode 100644 --- /dev/null +++ b/libc/config/linux/x86_64/machine_lowlevel.cpp @@ -0,0 +1,19 @@ +//===-- Implementation of x86_64 specific low level utils -----------------===// +// +// 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 + +namespace __llvm_libc { + +uintptr_t get_current_sp() { + uintptr_t *bp = reinterpret_cast(__builtin_frame_address(0)); + // If compiled with frame pointer, then the x86_64 ABI says ... + return reinterpret_cast(bp + 2); +} + +} // namespace __llvm_libc diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt --- a/libc/src/CMakeLists.txt +++ b/libc/src/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(__support) + add_subdirectory(assert) add_subdirectory(errno) add_subdirectory(math) @@ -9,5 +11,3 @@ add_subdirectory(sys) add_subdirectory(threads) add_subdirectory(unistd) - -add_subdirectory(__support) diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt --- a/libc/src/__support/CMakeLists.txt +++ b/libc/src/__support/CMakeLists.txt @@ -7,3 +7,15 @@ DATA_FILES ../../config/${LIBC_TARGET_OS}/platfrom_defs.h.inc ) + +add_gen_header( + lowlevel_utils + DEF_FILE + lowlevel_utils.h.def + PARAMS + platform_lowlevel=../../config/${LIBC_TARGET_OS}/platform_lowlevel.h.inc + GEN_HDR + lowlevel_utils.h + DATA_FILES + ../../config/${LIBC_TARGET_OS}/platform_lowlevel.h.inc +) diff --git a/libc/src/__support/lowlevel_utils.h.def b/libc/src/__support/lowlevel_utils.h.def new file mode 100644 --- /dev/null +++ b/libc/src/__support/lowlevel_utils.h.def @@ -0,0 +1,18 @@ +//===-- Platform independent lowlevel utils -------------------------------===// +// +// 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 + +namespace __llvm_libc { + +uintptr_t get_current_pc(); +uintptr_t get_current_sp(); + +} // namespace __llvm_libc + +%%include_file(${platform_lowlevel})