https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/241203
This PR move linkat behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. >From da1374dd39f5a77d63b13c9aa54075685b8e714c Mon Sep 17 00:00:00 2001 From: c8ef <[email protected]> Date: Wed, 11 Nov 2026 18:43:04 +0800 Subject: [PATCH] [libc] Use a syscall wrapper for linkat This PR move linkat behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. --- .../linux/syscall_wrappers/CMakeLists.txt | 12 +++++++ .../OSUtil/linux/syscall_wrappers/linkat.h | 34 +++++++++++++++++++ libc/src/unistd/linux/CMakeLists.txt | 8 ++--- libc/src/unistd/linux/linkat.cpp | 21 ++++++------ .../llvm-project-overlay/libc/BUILD.bazel | 19 +++++++++-- 5 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/linkat.h diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt index da7ab174a470b5..abe436804ac8a3 100644 --- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt @@ -1444,6 +1444,18 @@ add_header_library( libc.include.sys_syscall ) +add_header_library( + linkat + HDRS + linkat.h + DEPENDS + libc.include.sys_syscall + libc.src.__support.OSUtil.osutil + libc.src.__support.common + libc.src.__support.error_or + libc.src.__support.macros.config +) + add_header_library( uname HDRS diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/linkat.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/linkat.h new file mode 100644 index 00000000000000..01a5104ade705e --- /dev/null +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/linkat.h @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Syscall wrapper for linkat. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_LINKAT_H +#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_LINKAT_H + +#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked +#include "src/__support/common.h" +#include "src/__support/error_or.h" +#include "src/__support/macros/config.h" +#include <sys/syscall.h> // For syscall numbers. + +namespace LIBC_NAMESPACE_DECL { +namespace linux_syscalls { + +LIBC_INLINE ErrorOr<int> linkat(int fd1, const char *path1, int fd2, + const char *path2, int flags) { + return syscall_checked<int>(SYS_linkat, fd1, path1, fd2, path2, flags); +} + +} // namespace linux_syscalls +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_LINKAT_H diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt index 607f7b6230aec7..80517e2ece8536 100644 --- a/libc/src/unistd/linux/CMakeLists.txt +++ b/libc/src/unistd/linux/CMakeLists.txt @@ -639,10 +639,10 @@ add_entrypoint_object( HDRS ../linkat.h DEPENDS - libc.hdr.fcntl_macros - libc.include.unistd - libc.include.sys_syscall - libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.linkat + libc.src.__support.common + libc.src.__support.libc_errno + libc.src.__support.macros.config libc.src.errno.errno ) diff --git a/libc/src/unistd/linux/linkat.cpp b/libc/src/unistd/linux/linkat.cpp index ea5bc48cbedc59..69092e7359daae 100644 --- a/libc/src/unistd/linux/linkat.cpp +++ b/libc/src/unistd/linux/linkat.cpp @@ -1,33 +1,34 @@ -//===-- Linux implementation of linkat ------------------------------------===// +//===----------------------------------------------------------------------===// // // 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 // //===----------------------------------------------------------------------===// +/// +/// \file +/// Linux implementation of linkat. +/// +//===----------------------------------------------------------------------===// #include "src/unistd/linkat.h" -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/OSUtil/linux/syscall_wrappers/linkat.h" #include "src/__support/common.h" - -#include "hdr/fcntl_macros.h" #include "src/__support/libc_errno.h" #include "src/__support/macros/config.h" -#include <sys/syscall.h> // For syscall numbers. namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(int, linkat, (int fd1, const char *path1, int fd2, const char *path2, int flags)) { - int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_linkat, fd1, path1, fd2, - path2, flags); - if (ret < 0) { - libc_errno = -ret; + auto result = linux_syscalls::linkat(fd1, path1, fd2, path2, flags); + if (!result) { + libc_errno = result.error(); return -1; } - return ret; + return result.value(); } } // namespace LIBC_NAMESPACE_DECL diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index 8197745913ce92..a59084643858f3 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -2816,6 +2816,21 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_osutil_linux_syscall_wrappers_linkat", + hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/linkat.h"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + deps = [ + ":__support_common", + ":__support_error_or", + ":__support_macros_config", + ":__support_osutil_syscall", + ], +) + libc_support_library( name = "__support_osutil_linux_syscall_wrappers_fchdir", hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/fchdir.h"], @@ -19121,10 +19136,8 @@ libc_function( ":__support_common", ":__support_libc_errno", ":__support_macros_config", - ":__support_osutil_syscall", + ":__support_osutil_linux_syscall_wrappers_linkat", ":errno", - ":hdr_fcntl_macros", - ":hdr_unistd_macros", ], ) _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
