-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[libc] provide binary compatibility with syscall symbol #117229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
@llvm/pr-subscribers-libc Author: Schrodinger ZHU Yifan (SchrodingerZhu) ChangesFull diff: https://github.com/llvm/llvm-project/pull/117229.diff 9 Files Affected:
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 74ca3742977a5f..9a34029ab78230 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1002,6 +1002,7 @@ if(LLVM_LIBC_FULL_BUILD)
# unistd.h entrypoints
libc.src.unistd.__llvm_libc_syscall
+ libc.src.unistd.syscall_compat
libc.src.unistd._exit
libc.src.unistd.environ
libc.src.unistd.execv
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 5419462d4f5b3b..d7bdc6daa1b01c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -940,6 +940,7 @@ if(LLVM_LIBC_FULL_BUILD)
# unistd.h entrypoints
libc.src.unistd.__llvm_libc_syscall
+ libc.src.unistd.syscall_compat
libc.src.unistd._exit
libc.src.unistd.environ
libc.src.unistd.execv
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 957e28bd66cc4c..5b6aa83712c991 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1094,6 +1094,7 @@ if(LLVM_LIBC_FULL_BUILD)
# unistd.h entrypoints
libc.src.unistd.__llvm_libc_syscall
+ libc.src.unistd.syscall_compat
libc.src.unistd._exit
libc.src.unistd.environ
libc.src.unistd.execv
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 1a0b2e3293d03c..29f71ceca64750 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -252,6 +252,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.__llvm_libc_syscall
)
+add_entrypoint_object(
+ syscall_compat
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.syscall_compat
+)
+
add_entrypoint_object(
sysconf
ALIAS
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 8a448731414143..106477cec2b07d 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -465,6 +465,16 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ syscall_compat
+ SRCS
+ syscall_compat.cpp
+ HDRS
+ ../syscall_compat.h
+ DEPENDS
+ .__llvm_libc_syscall
+)
+
add_entrypoint_object(
sysconf
SRCS
diff --git a/libc/src/unistd/linux/syscall_compat.cpp b/libc/src/unistd/linux/syscall_compat.cpp
new file mode 100644
index 00000000000000..fc0039e15552f7
--- /dev/null
+++ b/libc/src/unistd/linux/syscall_compat.cpp
@@ -0,0 +1,32 @@
+//===-- Linux implementation of syscall (va compat) -----------------------===//
+//
+// 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 "src/unistd/syscall_compat.h"
+#include "src/__support/common.h"
+#include "src/unistd/syscall.h"
+
+#include "src/__support/macros/config.h"
+#include <stdarg.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+#undef syscall
+LLVM_LIBC_FUNCTION(long, syscall, (long n, ...)) {
+ va_list args;
+ va_start(args, n);
+ long arg1 = va_arg(args, long);
+ long arg2 = va_arg(args, long);
+ long arg3 = va_arg(args, long);
+ long arg4 = va_arg(args, long);
+ long arg5 = va_arg(args, long);
+ long arg6 = va_arg(args, long);
+ va_end(args);
+ return __llvm_libc_syscall(n, arg1, arg2, arg3, arg4, arg5, arg6);
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/syscall_compat.h b/libc/src/unistd/syscall_compat.h
new file mode 100644
index 00000000000000..4f2271ce52d139
--- /dev/null
+++ b/libc/src/unistd/syscall_compat.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for syscall (va compat) -----------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_UNISTD_SYSCALL_COMPAT_H
+#define LLVM_LIBC_SRC_UNISTD_SYSCALL_COMPAT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+#pragma push_macro("syscall")
+#undef syscall
+long syscall(long number, ...);
+#pragma pop_macro("syscall")
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_SYSCALL_COMPAT_H
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index e036e09cde702e..43491202626a1f 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -429,6 +429,7 @@ add_libc_unittest(
libc.include.sys_syscall
libc.src.errno.errno
libc.src.unistd.__llvm_libc_syscall
+ libc.src.unistd.syscall_compat
libc.test.UnitTest.ErrnoSetterMatcher
)
diff --git a/libc/test/src/unistd/syscall_test.cpp b/libc/test/src/unistd/syscall_test.cpp
index f6cc3eab9aabe8..9ac842267b4047 100644
--- a/libc/test/src/unistd/syscall_test.cpp
+++ b/libc/test/src/unistd/syscall_test.cpp
@@ -8,6 +8,7 @@
#include "src/errno/libc_errno.h"
#include "src/unistd/syscall.h"
+#include "src/unistd/syscall_compat.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
@@ -177,3 +178,12 @@ TEST(LlvmLibcSyscallTest, FileLinkCreateDestroy) {
ASSERT_GE(LIBC_NAMESPACE::syscall(SYS_close, dir_fd), 0l);
ASSERT_ERRNO_SUCCESS();
}
+
+TEST(LlvmLibcSyscallTest, TrivialCallCompat) {
+ LIBC_NAMESPACE::libc_errno = 0;
+#pragma push_macro("syscall")
+#undef syscall
+ ASSERT_GE(LIBC_NAMESPACE::syscall(SYS_gettid), 0l);
+#pragma pop_macro("syscall")
+ ASSERT_ERRNO_SUCCESS();
+}
|
|
Mind adding a description to this PR? |
|
Ah, sorry I missed it when committing. |
|
I'm not sure why this is necessary. We already have an implementation of It's got a different name that's translated through a macro in the |
|
For example, rust std assumes syscall. |
I think the problem is that even though we do not assume ABI compatibility with glibc compiled programs, in current approach, we are not providing standard means to programs outside C/C++ to do platform-agnostic syscall. |
|
IIUC, it looks like the rust std lib is expecting to use C FFI to do syscalls: https://codebrowser.dev/rust/rust/library/std/src/sys/pal/unix/weak.rs.html#158 IIUC, it doesn't matter if our syscall is a macro, if the runtime of the higher level language expects syscall to be a symbol and can't use the C preprocessor (such as rust). |
|
@nickdesaulniers yes, but maybe we should ask them to change instead if they are to support LLVM's libc? I am open for discussion. e.g. they can use |
Glibc and MUSL both provides
syscallas a binary. Inside LLVM-libc, we provides it as a macro backed by__llvm_libc_syscallwhich is not binary compatible with other implementations. This PR provides the symbol for better compatibility for those applications assuming the existence of the symbol.