[llvm-branch-commits] [libc] [llvm] [libc] Use a syscall wrapper for symlink (PR #241204)
Connector Switch via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Nov 11 02:44:32 PST 2026
https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/241204
This PR move symlink behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
>From 77f815e69fc68b9bcdaeb1daecb13b09f7c51097 Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 11 Nov 2026 18:43:27 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for symlink
This PR move symlink behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
.../linux/syscall_wrappers/CMakeLists.txt | 13 ++++++
.../OSUtil/linux/syscall_wrappers/symlink.h | 40 +++++++++++++++++++
libc/src/unistd/linux/CMakeLists.txt | 9 +++--
libc/src/unistd/linux/symlink.cpp | 27 +++++--------
.../llvm-project-overlay/libc/BUILD.bazel | 19 ++++++++-
5 files changed, 86 insertions(+), 22 deletions(-)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/symlink.h
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index abe436804ac8a3..3e4042ae57d6d6 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1456,6 +1456,19 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ symlink
+ HDRS
+ symlink.h
+ DEPENDS
+ libc.hdr.fcntl_macros
+ 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/symlink.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/symlink.h
new file mode 100644
index 00000000000000..732714d1fe6faa
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/symlink.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 symlink.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYMLINK_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYMLINK_H
+
+#include "hdr/fcntl_macros.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> symlink(const char *path1, const char *path2) {
+#ifdef SYS_symlink
+ return syscall_checked<int>(SYS_symlink, path1, path2);
+#elif defined(SYS_symlinkat)
+ return syscall_checked<int>(SYS_symlinkat, path1, AT_FDCWD, path2);
+#else
+#error "symlink or symlinkat syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SYMLINK_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 80517e2ece8536..b9ed2e26c09dc8 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -1015,10 +1015,11 @@ add_entrypoint_object(
HDRS
../symlink.h
DEPENDS
- libc.hdr.fcntl_macros
- libc.include.unistd
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.hdr.unistd_macros
+ libc.src.__support.OSUtil.linux.syscall_wrappers.symlink
+ 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/symlink.cpp b/libc/src/unistd/linux/symlink.cpp
index 3f43de19d2f46f..e28adf57496281 100644
--- a/libc/src/unistd/linux/symlink.cpp
+++ b/libc/src/unistd/linux/symlink.cpp
@@ -1,37 +1,32 @@
-//===-- Linux implementation of symlink -----------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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 symlink.
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/symlink.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/symlink.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include "hdr/fcntl_macros.h"
-#include <sys/syscall.h> // For syscall numbers.
-
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, symlink, (const char *path1, const char *path2)) {
-#ifdef SYS_symlink
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_symlink, path1, path2);
-#elif defined(SYS_symlinkat)
- int ret =
- LIBC_NAMESPACE::syscall_impl<int>(SYS_symlinkat, path1, AT_FDCWD, path2);
-#else
-#error "symlink or symlinkat syscalls not available."
-#endif
- if (ret < 0) {
- libc_errno = -ret;
+ auto result = linux_syscalls::symlink(path1, path2);
+ 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 a59084643858f3..cd565098affdcf 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -3339,6 +3339,22 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_symlink",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/symlink.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ":hdr_fcntl_macros",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_uname",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/uname.h"],
@@ -19308,9 +19324,8 @@ libc_function(
":__support_common",
":__support_libc_errno",
":__support_macros_config",
- ":__support_osutil_syscall",
+ ":__support_osutil_linux_syscall_wrappers_symlink",
":errno",
- ":hdr_fcntl_macros",
":hdr_unistd_macros",
],
)
More information about the llvm-branch-commits
mailing list