From eb521738bd7ce3ff9df1838a704e4a3e984255bb Mon Sep 17 00:00:00 2001 From: Izaak Schroeder Date: Mon, 1 Jul 2024 16:23:01 -0700 Subject: [PATCH] [libc] Add `dlfcn.h` placeholder --- libc/config/linux/aarch64/entrypoints.txt | 6 +++ libc/config/linux/aarch64/headers.txt | 1 + libc/config/linux/x86_64/entrypoints.txt | 6 +++ 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 | 19 +++++++++ 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, 290 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 940df63e3912b7..70c588622ec6b2 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -17,6 +17,12 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.ctype.tolower libc.src.ctype.toupper + # dlfcn.h entrypoints + libc.src.dlfcn.dlopen + libc.src.dlfcn.dlsym + libc.src.dlfcn.dlclose + libc.src.dlfcn.dlerror + # errno.h entrypoints libc.src.errno.errno diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt index 7d25877cefcc83..8f898f0150905a 100644 --- a/libc/config/linux/aarch64/headers.txt +++ b/libc/config/linux/aarch64/headers.txt @@ -1,6 +1,7 @@ set(TARGET_PUBLIC_HEADERS libc.include.assert libc.include.ctype + libc.include.dlfcn libc.include.errno libc.include.features libc.include.fenv diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index 09f04fb31dfd82..ba60475809853f 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -17,6 +17,12 @@ set(TARGET_LIBC_ENTRYPOINTS libc.src.ctype.tolower libc.src.ctype.toupper + # dlfcn.h entrypoints + libc.src.dlfcn.dlopen + libc.src.dlfcn.dlsym + libc.src.dlfcn.dlclose + libc.src.dlfcn.dlerror + # errno.h entrypoints libc.src.errno.errno diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt index 44d640b75e2bf7..df276894246c4c 100644 --- a/libc/config/linux/x86_64/headers.txt +++ b/libc/config/linux/x86_64/headers.txt @@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS libc.include.assert libc.include.ctype libc.include.dirent + libc.include.dlfcn libc.include.errno libc.include.fcntl libc.include.features diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt index 3ab7817d8568b7..f8ef35078a8c46 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 00000000000000..31395871c6b97e --- /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 f6af11abd4dd76..5bf573e4e98dfe 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 00000000000000..dcd202b9ab435c --- /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 d14047548e104f..1878b1ee2ae412 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, + [ArgSpec] + >, + FunctionSpec< + "dlerror", + RetValSpec, + [] + >, + FunctionSpec< + "dlopen", + RetValSpec, + [ArgSpec, ArgSpec] + >, + FunctionSpec< + "dlsym", + RetValSpec, + [ArgSpec, ArgSpec] + >, + ] + >; + 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 09b16be1e2d42e..f011fe25226e55 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 00000000000000..5534f2dfb88533 --- /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 00000000000000..6dc1892120dddb --- /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 00000000000000..27c0207d726e49 --- /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 00000000000000..8c3611d9c639e5 --- /dev/null +++ b/libc/src/dlfcn/dlerror.cpp @@ -0,0 +1,19 @@ +//===-- 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 const_cast("unsupported"); +} + +} // namespace LIBC_NAMESPACE diff --git a/libc/src/dlfcn/dlerror.h b/libc/src/dlfcn/dlerror.h new file mode 100644 index 00000000000000..966496016d3ebd --- /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 00000000000000..835d981e192371 --- /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 00000000000000..4565953efd4943 --- /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 00000000000000..98d678caece902 --- /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 00000000000000..8157ac3e3fd4ca --- /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