[libc] [libunwind] [WIP] Enough to get `libc` to build `clang` on `aarch64` (PR #97231)

Izaak Schroeder via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 1 17:19:40 PDT 2024


https://github.com/izaakschroeder updated https://github.com/llvm/llvm-project/pull/97231

>From 31dc769c1866a4a0100dde0a3743c215a9a5f5eb Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Sun, 30 Jun 2024 13:33:16 -0700
Subject: [PATCH 01/10] [libc]: add missing aarch64 headers

---
 libc/config/linux/aarch64/entrypoints.txt | 3 +++
 libc/config/linux/aarch64/headers.txt     | 6 ++++++
 2 files changed, 9 insertions(+)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 8a26536cea9a0..b59400db81fed 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -308,6 +308,9 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.unlink
     libc.src.unistd.unlinkat
     libc.src.unistd.write
+
+    # XXX
+    libc.src.assert.__assert_fail
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 7d25877cefcc8..ed126d6128871 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -31,4 +31,10 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_ioctl
     # Disabled due to epoll_wait syscalls not being available on this platform.
     # libc.include.sys_epoll
+
+    # XXX
+    libc.include.sys_auxv
+    libc.include.fcntl
+    libc.include.sched
+    libc.include.sys_stat
 )

>From a5f8ee220c088a39d9b4bf60986166e83ffafffa Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Sun, 30 Jun 2024 13:33:38 -0700
Subject: [PATCH 02/10] [libunwind]: remove needless `sys/uio.h`

No reference to `readv` or `writev`.
---
 libunwind/src/UnwindCursor.hpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libunwind/src/UnwindCursor.hpp b/libunwind/src/UnwindCursor.hpp
index 66fe8e2a32cca..677e842d8a22b 100644
--- a/libunwind/src/UnwindCursor.hpp
+++ b/libunwind/src/UnwindCursor.hpp
@@ -36,7 +36,6 @@
 #include <errno.h>
 #include <signal.h>
 #include <sys/syscall.h>
-#include <sys/uio.h>
 #include <unistd.h>
 #define _LIBUNWIND_CHECK_LINUX_SIGRETURN 1
 #endif

>From 1083aecfe1be3c1cf427d4ffd2b4fad3d5129d4c Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Sun, 30 Jun 2024 13:44:52 -0700
Subject: [PATCH 03/10] [libc]: function spec for `getauxval`

---
 libc/spec/gnu_ext.td | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/libc/spec/gnu_ext.td b/libc/spec/gnu_ext.td
index 161bb4e4a0d9d..e360c766c5c54 100644
--- a/libc/spec/gnu_ext.td
+++ b/libc/spec/gnu_ext.td
@@ -237,7 +237,11 @@ def GnuExtensions : StandardSpec<"GNUExtensions"> {
       [], // Types
       [], // Enumerations
       [
-        //TODO: Add getauxval here
+          FunctionSpec<
+              "getauxval",
+              RetValSpec<UnsignedLongType>,
+              [ArgSpec<UnsignedLongType>]
+          >,
       ]  // Functions
   >;
 

>From e9628ce037c225c3769cec2589745f7978e7edc4 Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Sun, 30 Jun 2024 14:06:19 -0700
Subject: [PATCH 04/10] [libc]: hack `ExitCallbackList` always public

---
 libc/src/stdlib/exit_handler.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/libc/src/stdlib/exit_handler.h b/libc/src/stdlib/exit_handler.h
index 8494c2f2e526e..db731a46f44c9 100644
--- a/libc/src/stdlib/exit_handler.h
+++ b/libc/src/stdlib/exit_handler.h
@@ -31,10 +31,8 @@ struct AtExitUnit {
 
 #if defined(LIBC_TARGET_ARCH_IS_GPU)
 using ExitCallbackList = FixedVector<AtExitUnit, 64>;
-#elif defined(LIBC_COPT_PUBLIC_PACKAGING)
-using ExitCallbackList = ReverseOrderBlockStore<AtExitUnit, 32>;
 #else
-using ExitCallbackList = FixedVector<AtExitUnit, CALLBACK_LIST_SIZE_FOR_TESTS>;
+using ExitCallbackList = ReverseOrderBlockStore<AtExitUnit, 32>;
 #endif
 
 extern ExitCallbackList atexit_callbacks;

>From e3ed9aac99ede8be5d6d228f43c652423206e67c Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Sun, 30 Jun 2024 14:15:21 -0700
Subject: [PATCH 05/10] [libc]: add `Scrt1.o`

---
 libc/startup/linux/CMakeLists.txt | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 336c5d0f6bfa2..b721d15091227 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -118,6 +118,15 @@ merge_relocatable_object(
   .do_start
 )
 
+# HACK: Is this even ok?
+# For reference: `Scrt1` is used when `-fpie` vs `crt1` for `-fno-pie`
+merge_relocatable_object(
+  Scrt1
+  .${LIBC_TARGET_ARCHITECTURE}.start
+  .${LIBC_TARGET_ARCHITECTURE}.tls
+  .do_start
+)
+
 add_startup_object(
   crti
   SRC
@@ -131,7 +140,7 @@ add_startup_object(
 )
 
 add_custom_target(libc-startup)
-set(startup_components crt1 crti crtn)
+set(startup_components Scrt1 crt1 crti crtn)
 foreach(target IN LISTS startup_components)
   set(fq_target_name libc.startup.linux.${target})
   add_dependencies(libc-startup ${fq_target_name})

>From d4257c176073abccec419cb5cf65afaa99fa6bcf Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Mon, 1 Jul 2024 12:07:37 -0700
Subject: [PATCH 06/10] [libc]: `sys_time.utimes` placeholder

---
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  3 +++
 libc/spec/linux.td                        | 12 +++++++++++-
 libc/spec/spec.td                         |  1 +
 libc/src/sys/CMakeLists.txt               |  1 +
 libc/src/sys/time/CMakeLists.txt          | 10 ++++++++++
 libc/src/sys/time/linux/CMakeLists.txt    | 12 ++++++++++++
 libc/src/sys/time/linux/utimes.cpp        | 12 ++++++++++++
 libc/src/sys/time/utimes.h                | 18 ++++++++++++++++++
 9 files changed, 69 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/sys/time/CMakeLists.txt
 create mode 100644 libc/src/sys/time/linux/CMakeLists.txt
 create mode 100644 libc/src/sys/time/linux/utimes.cpp
 create mode 100644 libc/src/sys/time/utimes.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b59400db81fed..f259f30f8b377 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -311,6 +311,7 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # XXX
     libc.src.assert.__assert_fail
+    libc.src.sys_time.utimes
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index e1922ca94b97e..05ab170127a9d 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -330,6 +330,9 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # wchar.h entrypoints
     libc.src.wchar.wctob
+
+    # XXX
+    libc.src.sys_time.utimes
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/spec/linux.td b/libc/spec/linux.td
index 82630ff413c73..faeefd70f2263 100644
--- a/libc/spec/linux.td
+++ b/libc/spec/linux.td
@@ -148,7 +148,17 @@ def Linux : StandardSpec<"Linux"> {
       ],
       [StructTimevalType], // Types
       [], // Enumerations
-      []  // Functions
+      [
+        FunctionSpec<
+          "utimes",
+          RetValSpec<IntType>,
+          [
+            ArgSpec<IntType>,
+            ArgSpec<ConstCharPtr>,
+            ArgSpec<ConstStructTimevalPtr>,
+          ]
+        >,
+      ]  // Functions
   >;
 
 
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index a3a5db7465b39..17c0589ab1d55 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -148,6 +148,7 @@ def StructRUsagePtr : PtrType<StructRUsage>;
 
 def StructTimevalType : NamedType<"struct timeval">;
 def StructTimevalPtr : PtrType<StructTimevalType>;
+def ConstStructTimevalPtr : ConstType<StructTimevalPtr>;
 def RestrictedStructTimevalPtr : RestrictedPtrType<StructTimevalType>;
 
 def SuSecondsT : NamedType<"suseconds_t">;
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index adc666b94202f..fba5f86d0b08c 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -11,3 +11,4 @@ add_subdirectory(statvfs)
 add_subdirectory(utsname)
 add_subdirectory(wait)
 add_subdirectory(prctl)
+add_subdirectory(time)
diff --git a/libc/src/sys/time/CMakeLists.txt b/libc/src/sys/time/CMakeLists.txt
new file mode 100644
index 0000000000000..f599cddaaeeb3
--- /dev/null
+++ b/libc/src/sys/time/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  utimes
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.utimes
+)
diff --git a/libc/src/sys/time/linux/CMakeLists.txt b/libc/src/sys/time/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..da5f698e3515c
--- /dev/null
+++ b/libc/src/sys/time/linux/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_entrypoint_object(
+  utimes
+  SRCS
+    utimes.cpp
+  HDRS
+    ../utimes.h
+  DEPENDS
+    libc.include.sys_time
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/time/linux/utimes.cpp b/libc/src/sys/time/linux/utimes.cpp
new file mode 100644
index 0000000000000..0bab9ada0b2bf
--- /dev/null
+++ b/libc/src/sys/time/linux/utimes.cpp
@@ -0,0 +1,12 @@
+#include "src/__support/common.h"
+#include "src/errno/libc_errno.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, utimes,
+                   (int dirfd, const char *pathname,
+                    const struct timeval times[2])) {
+  return EINVAL;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/sys/time/utimes.h b/libc/src/sys/time/utimes.h
new file mode 100644
index 0000000000000..eb2bcc3f15f25
--- /dev/null
+++ b/libc/src/sys/time/utimes.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for socket ------------------------*- 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_SYS_TIME_UTIMES_H
+#define LLVM_LIBC_SRC_SYS_TIME_UTIMES_H
+
+namespace LIBC_NAMESPACE {
+
+int utimes(int dirfd, const char *pathname, const struct timeval times[2]);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_SYS_TIME_UTIMES_H

>From 3f1cdb78be8624cccddfbf53b066f88cfe181e2e Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Mon, 1 Jul 2024 13:13:16 -0700
Subject: [PATCH 07/10] [libc]: add missing `<sys/statvfs.h>` and friends

---
 libc/config/linux/aarch64/entrypoints.txt | 2 ++
 libc/config/linux/aarch64/headers.txt     | 1 +
 libc/config/linux/x86_64/headers.txt      | 3 +++
 3 files changed, 6 insertions(+)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f259f30f8b377..e2a8a8de378b1 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -312,6 +312,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     # XXX
     libc.src.assert.__assert_fail
     libc.src.sys_time.utimes
+    libc.src.sys.statvfs.fstatvfs
+    libc.src.sys.statvfs.statvfs
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index ed126d6128871..b310da522a50c 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -34,6 +34,7 @@ set(TARGET_PUBLIC_HEADERS
 
     # XXX
     libc.include.sys_auxv
+    libc.include.sys_statvfs
     libc.include.fcntl
     libc.include.sched
     libc.include.sys_stat
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 44d640b75e2bf..7e9d26c4821fc 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -49,4 +49,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.sys_types
     libc.include.sys_utsname
     libc.include.sys_wait
+
+    # XXX
+    libc.include.sys_statvfs
 )

>From 80a713fc0db0f948555f52bcb54956662343c20a Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Mon, 1 Jul 2024 13:14:21 -0700
Subject: [PATCH 08/10] [libc]: add basic `ioctl` function

---
 libc/config/linux/aarch64/entrypoints.txt |  3 ++-
 libc/config/linux/x86_64/entrypoints.txt  |  3 ++-
 libc/src/sys/CMakeLists.txt               |  1 +
 libc/src/sys/ioctl/CMakeLists.txt         | 10 ++++++++++
 libc/src/sys/ioctl/ioctl.h                | 18 ++++++++++++++++++
 libc/src/sys/ioctl/linux/CMakeLists.txt   | 12 ++++++++++++
 libc/src/sys/ioctl/linux/ioctl.cpp        | 23 +++++++++++++++++++++++
 7 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 libc/src/sys/ioctl/CMakeLists.txt
 create mode 100644 libc/src/sys/ioctl/ioctl.h
 create mode 100644 libc/src/sys/ioctl/linux/CMakeLists.txt
 create mode 100644 libc/src/sys/ioctl/linux/ioctl.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e2a8a8de378b1..f58debcf9f38c 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -311,9 +311,10 @@ set(TARGET_LIBC_ENTRYPOINTS
 
     # XXX
     libc.src.assert.__assert_fail
-    libc.src.sys_time.utimes
+    libc.src.sys.time.utimes
     libc.src.sys.statvfs.fstatvfs
     libc.src.sys.statvfs.statvfs
+    libc.src.sys.ioctl.ioctl
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 05ab170127a9d..ba88ee253230f 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -332,7 +332,8 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.wchar.wctob
 
     # XXX
-    libc.src.sys_time.utimes
+    libc.src.sys.time.utimes
+    libc.src.sys.ioctl.ioctl
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index fba5f86d0b08c..de8677e9cc933 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -11,4 +11,5 @@ add_subdirectory(statvfs)
 add_subdirectory(utsname)
 add_subdirectory(wait)
 add_subdirectory(prctl)
+add_subdirectory(ioctl)
 add_subdirectory(time)
diff --git a/libc/src/sys/ioctl/CMakeLists.txt b/libc/src/sys/ioctl/CMakeLists.txt
new file mode 100644
index 0000000000000..099a1b96389fc
--- /dev/null
+++ b/libc/src/sys/ioctl/CMakeLists.txt
@@ -0,0 +1,10 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  ioctl
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.ioctl
+)
diff --git a/libc/src/sys/ioctl/ioctl.h b/libc/src/sys/ioctl/ioctl.h
new file mode 100644
index 0000000000000..1d4789146c40f
--- /dev/null
+++ b/libc/src/sys/ioctl/ioctl.h
@@ -0,0 +1,18 @@
+//===-- Implementation header for socket ------------------------*- 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_SYS_IOCTL_IOCTL_H
+#define LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H
+
+namespace LIBC_NAMESPACE {
+
+int ioctl(int fd, int req, ...);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_SYS_IOCTL_IOCTL_H
diff --git a/libc/src/sys/ioctl/linux/CMakeLists.txt b/libc/src/sys/ioctl/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..876f35aaee66c
--- /dev/null
+++ b/libc/src/sys/ioctl/linux/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_entrypoint_object(
+  ioctl
+  SRCS
+    ioctl.cpp
+  HDRS
+    ../ioctl.h
+  DEPENDS
+    libc.include.sys_ioctl
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/ioctl/linux/ioctl.cpp b/libc/src/sys/ioctl/linux/ioctl.cpp
new file mode 100644
index 0000000000000..5cd8f3ec3eb29
--- /dev/null
+++ b/libc/src/sys/ioctl/linux/ioctl.cpp
@@ -0,0 +1,23 @@
+#include <stdarg.h>
+#include <sys/syscall.h> // For syscall numbers.
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/common.h"
+
+#include "src/errno/libc_errno.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, ioctl, (int fd, int req, ...)) {
+  void *arg;
+  va_list ap;
+  va_start(ap, req);
+  arg = va_arg(ap, void *);
+  va_end(ap);
+  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, req, arg);
+  // FIXME(@izaakschroeder): There is probably more to do here.
+  // See: https://github.com/kraj/musl/blob/kraj/master/src/misc/ioctl.c
+  return ret;
+}
+
+} // namespace LIBC_NAMESPACE

>From 6568d566fd2b34fc8a57eaa77b6182748e55405d Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Mon, 1 Jul 2024 16:23:01 -0700
Subject: [PATCH 09/10] [libc]: add `dlfcn.h` placeholder

---
 libc/config/linux/aarch64/entrypoints.txt    |  4 ++
 libc/config/linux/aarch64/headers.txt        |  1 +
 libc/config/linux/x86_64/entrypoints.txt     |  4 ++
 libc/config/linux/x86_64/headers.txt         |  1 +
 libc/include/CMakeLists.txt                  |  9 ++++
 libc/include/dlfcn.h.def                     | 17 ++++++++
 libc/include/llvm-libc-macros/CMakeLists.txt |  6 +++
 libc/include/llvm-libc-macros/dlfcn-macros.h | 23 +++++++++++
 libc/spec/posix.td                           | 35 ++++++++++++++++
 libc/src/CMakeLists.txt                      |  1 +
 libc/src/dlfcn/CMakeLists.txt                | 43 ++++++++++++++++++++
 libc/src/dlfcn/dlclose.cpp                   | 17 ++++++++
 libc/src/dlfcn/dlclose.h                     | 18 ++++++++
 libc/src/dlfcn/dlerror.cpp                   | 17 ++++++++
 libc/src/dlfcn/dlerror.h                     | 18 ++++++++
 libc/src/dlfcn/dlopen.cpp                    | 17 ++++++++
 libc/src/dlfcn/dlopen.h                      | 18 ++++++++
 libc/src/dlfcn/dlsym.cpp                     | 17 ++++++++
 libc/src/dlfcn/dlsym.h                       | 18 ++++++++
 19 files changed, 284 insertions(+)
 create mode 100644 libc/include/dlfcn.h.def
 create mode 100644 libc/include/llvm-libc-macros/dlfcn-macros.h
 create mode 100644 libc/src/dlfcn/CMakeLists.txt
 create mode 100644 libc/src/dlfcn/dlclose.cpp
 create mode 100644 libc/src/dlfcn/dlclose.h
 create mode 100644 libc/src/dlfcn/dlerror.cpp
 create mode 100644 libc/src/dlfcn/dlerror.h
 create mode 100644 libc/src/dlfcn/dlopen.cpp
 create mode 100644 libc/src/dlfcn/dlopen.h
 create mode 100644 libc/src/dlfcn/dlsym.cpp
 create mode 100644 libc/src/dlfcn/dlsym.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f58debcf9f38c..1d7dde40e371e 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -315,6 +315,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.statvfs.fstatvfs
     libc.src.sys.statvfs.statvfs
     libc.src.sys.ioctl.ioctl
+    libc.src.dlfcn.dlopen
+    libc.src.dlfcn.dlsym
+    libc.src.dlfcn.dlclose
+    libc.src.dlfcn.dlerror
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index b310da522a50c..4f47b09b26b77 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -38,4 +38,5 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.fcntl
     libc.include.sched
     libc.include.sys_stat
+    libc.include.dlfcn
 )
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index ba88ee253230f..e5feb4ebed805 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -334,6 +334,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     # XXX
     libc.src.sys.time.utimes
     libc.src.sys.ioctl.ioctl
+    libc.src.dlfcn.dlopen
+    libc.src.dlfcn.dlsym
+    libc.src.dlfcn.dlclose
+    libc.src.dlfcn.dlerror
 )
 
 set(TARGET_LIBM_ENTRYPOINTS
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 7e9d26c4821fc..16af88b54e4ab 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -52,4 +52,5 @@ set(TARGET_PUBLIC_HEADERS
 
     # XXX
     libc.include.sys_statvfs
+    libc.include.dlfcn
 )
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 3ab7817d8568b..f8ef35078a8c4 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -51,6 +51,15 @@ add_gen_header(
     .llvm_libc_common_h
 )
 
+add_gen_header(
+  dlfcn
+  DEF_FILE dlfcn.h.def
+  GEN_HDR dlfcn.h
+  DEPENDS
+    .llvm-libc-macros.dlfcn_macros
+    .llvm_libc_common_h
+)
+
 add_gen_header(
   features
   DEF_FILE features.h.def
diff --git a/libc/include/dlfcn.h.def b/libc/include/dlfcn.h.def
new file mode 100644
index 0000000000000..31395871c6b97
--- /dev/null
+++ b/libc/include/dlfcn.h.def
@@ -0,0 +1,17 @@
+//===-- C standard library header dlfcn.h ---------------------------------===//
+//
+// 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_DLFCN_H
+#define LLVM_LIBC_DLFCN_H
+
+#include "__llvm-libc-common.h"
+#include "llvm-libc-macros/dlfcn-macros.h"
+
+%%public_api()
+
+#endif // LLVM_LIBC_DLFCN_H
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index f6af11abd4dd7..5bf573e4e98df 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -277,3 +277,9 @@ add_macro_header(
   HDR
     stdckdint-macros.h
 )
+
+add_macro_header(
+  dlfcn_macros
+  HDR
+    dlfcn-macros.h
+)
\ No newline at end of file
diff --git a/libc/include/llvm-libc-macros/dlfcn-macros.h b/libc/include/llvm-libc-macros/dlfcn-macros.h
new file mode 100644
index 0000000000000..dcd202b9ab435
--- /dev/null
+++ b/libc/include/llvm-libc-macros/dlfcn-macros.h
@@ -0,0 +1,23 @@
+//===-- Definition of macros from dlfcn.h ---------------------------------===//
+//
+// 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_MACROS_DLFCN_MACROS_H
+#define LLVM_LIBC_MACROS_DLFCN_MACROS_H
+
+#define RTLD_LAZY 0x00001
+#define RTLD_NOW 0x00002
+#define RTLD_GLOBAL 0x00100
+#define RTLD_LOCAL 0
+
+// Non-standard stuff here
+#define RTLD_BINDING_MASK 0x3
+#define RTLD_NOLOAD 0x00004
+#define RTLD_DEEPBIND 0x00008
+#define RTLD_NODELETE 0x01000
+
+#endif // LLVM_LIBC_MACROS_DLFCN_MACROS_H
diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index d14047548e104..6bdd2d7100073 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -222,6 +222,40 @@ def POSIX : StandardSpec<"POSIX"> {
       []  // Functions
   >;
 
+  HeaderSpec DlFcn = HeaderSpec<
+    "dlfcn.h",
+    [
+      Macro<"RTLD_LAZY">,
+      Macro<"RTLD_NOW">,
+      Macro<"RTLD_GLOBAL">,
+      Macro<"RTLD_LOCAL">,
+    ],
+    [],  // Types
+    [], // Enumerations
+    [
+      FunctionSpec<
+          "dlclose",
+          RetValSpec<IntType>,
+          [ArgSpec<VoidPtr>]
+      >,
+      FunctionSpec<
+          "dlerror",
+          RetValSpec<CharPtr>,
+          []
+      >,
+      FunctionSpec<
+          "dlopen",
+          RetValSpec<VoidPtr>,
+          [ArgSpec<ConstCharPtr>, ArgSpec<IntType>]
+      >,
+      FunctionSpec<
+          "dlsym",
+          RetValSpec<VoidPtr>,
+          [ArgSpec<VoidRestrictedPtr>, ArgSpec<ConstCharRestrictedPtr>]
+      >,
+    ],
+  >;
+
   HeaderSpec FCntl = HeaderSpec<
     "fcntl.h",
     [], // Macros
@@ -1690,6 +1724,7 @@ def POSIX : StandardSpec<"POSIX"> {
     ArpaInet,
     CType,
     Dirent,
+    DlFcn,
     Errno,
     FCntl,
     PThread,
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 09b16be1e2d42..f011fe25226e5 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -11,6 +11,7 @@ add_subdirectory(stdio)
 add_subdirectory(stdlib)
 add_subdirectory(string)
 add_subdirectory(wchar)
+add_subdirectory(dlfcn)
 
 if(${LIBC_TARGET_OS} STREQUAL "linux")
   add_subdirectory(dirent)
diff --git a/libc/src/dlfcn/CMakeLists.txt b/libc/src/dlfcn/CMakeLists.txt
new file mode 100644
index 0000000000000..5534f2dfb8853
--- /dev/null
+++ b/libc/src/dlfcn/CMakeLists.txt
@@ -0,0 +1,43 @@
+add_entrypoint_object(
+  dlclose
+  SRCS
+    dlclose.cpp
+  HDRS
+    dlclose.h
+  DEPENDS
+    libc.include.dlfcn
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  dlerror
+  SRCS
+    dlerror.cpp
+  HDRS
+    dlerror.h
+  DEPENDS
+    libc.include.dlfcn
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  dlopen
+  SRCS
+    dlopen.cpp
+  HDRS
+    dlopen.h
+  DEPENDS
+    libc.include.dlfcn
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  dlsym
+  SRCS
+    dlsym.cpp
+  HDRS
+    dlsym.h
+  DEPENDS
+    libc.include.dlfcn
+    libc.src.errno.errno
+)
diff --git a/libc/src/dlfcn/dlclose.cpp b/libc/src/dlfcn/dlclose.cpp
new file mode 100644
index 0000000000000..6dc1892120ddd
--- /dev/null
+++ b/libc/src/dlfcn/dlclose.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of dlclose -----------------------------------------===//
+//
+// 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 "dlclose.h"
+
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, dlclose, (void *)) { return -1; }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/dlfcn/dlclose.h b/libc/src/dlfcn/dlclose.h
new file mode 100644
index 0000000000000..27c0207d726e4
--- /dev/null
+++ b/libc/src/dlfcn/dlclose.h
@@ -0,0 +1,18 @@
+//===-- Implementation header of dlclose ------------------------*- 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_DLFCN_DLCLOSE_H
+#define LLVM_LIBC_SRC_DLFCN_DLCLOSE_H
+
+namespace LIBC_NAMESPACE {
+
+int dlclose(void *);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_DLFCN_DLCLOSE_H
diff --git a/libc/src/dlfcn/dlerror.cpp b/libc/src/dlfcn/dlerror.cpp
new file mode 100644
index 0000000000000..b1ba94394eda6
--- /dev/null
+++ b/libc/src/dlfcn/dlerror.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of delerror ----------------------------------------===//
+//
+// 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 "dlerror.h"
+
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(char *, dlerror, ()) { return "unsupported"; }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/dlfcn/dlerror.h b/libc/src/dlfcn/dlerror.h
new file mode 100644
index 0000000000000..966496016d3eb
--- /dev/null
+++ b/libc/src/dlfcn/dlerror.h
@@ -0,0 +1,18 @@
+//===-- Implementation header of dlerror ------------------------*- 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_DLFCN_DLERROR_H
+#define LLVM_LIBC_SRC_DLFCN_DLERROR_H
+
+namespace LIBC_NAMESPACE {
+
+char *dlerror();
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_DLFCN_DLERROR_H
diff --git a/libc/src/dlfcn/dlopen.cpp b/libc/src/dlfcn/dlopen.cpp
new file mode 100644
index 0000000000000..835d981e19237
--- /dev/null
+++ b/libc/src/dlfcn/dlopen.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of dlopen -----------------------------------------===//
+//
+// 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 "dlopen.h"
+
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(void *, dlopen, (const char *, int)) { return nullptr; }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/dlfcn/dlopen.h b/libc/src/dlfcn/dlopen.h
new file mode 100644
index 0000000000000..4565953efd494
--- /dev/null
+++ b/libc/src/dlfcn/dlopen.h
@@ -0,0 +1,18 @@
+//===-- Implementation header of dlopen -------------------------*- 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_DLFCN_DLOPEN_H
+#define LLVM_LIBC_SRC_DLFCN_DLOPEN_H
+
+namespace LIBC_NAMESPACE {
+
+void *dlopen(const char *, int);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_DLFCN_DLOPEN_H
diff --git a/libc/src/dlfcn/dlsym.cpp b/libc/src/dlfcn/dlsym.cpp
new file mode 100644
index 0000000000000..98d678caece90
--- /dev/null
+++ b/libc/src/dlfcn/dlsym.cpp
@@ -0,0 +1,17 @@
+//===-- Implementation of dlsym ------------------------------------------===//
+//
+// 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 "dlsym.h"
+
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(void *, dlsym, (void *, const char *)) { return nullptr; }
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/dlfcn/dlsym.h b/libc/src/dlfcn/dlsym.h
new file mode 100644
index 0000000000000..8157ac3e3fd4c
--- /dev/null
+++ b/libc/src/dlfcn/dlsym.h
@@ -0,0 +1,18 @@
+//===-- Implementation header of dlsym --------------------------*- 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_DLFCN_DLSYM_H
+#define LLVM_LIBC_SRC_DLFCN_DLSYM_H
+
+namespace LIBC_NAMESPACE {
+
+void *dlsym(void *, const char *);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_DLFCN_DLSYM_H

>From d913ad4556d26754a2add6b28e40398a9933e83b Mon Sep 17 00:00:00 2001
From: Izaak Schroeder <izaak.schroeder at gmail.com>
Date: Mon, 1 Jul 2024 17:19:10 -0700
Subject: [PATCH 10/10] fixup! remove trailing comma

---
 libc/spec/posix.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/spec/posix.td b/libc/spec/posix.td
index 6bdd2d7100073..1878b1ee2ae41 100644
--- a/libc/spec/posix.td
+++ b/libc/spec/posix.td
@@ -253,7 +253,7 @@ def POSIX : StandardSpec<"POSIX"> {
           RetValSpec<VoidPtr>,
           [ArgSpec<VoidRestrictedPtr>, ArgSpec<ConstCharRestrictedPtr>]
       >,
-    ],
+    ]
   >;
 
   HeaderSpec FCntl = HeaderSpec<



More information about the cfe-commits mailing list