Skip to content

Commit b151c7e

Browse files
[libc] Add dlfcn.h placeholder (#97501)
Adds `dlopen` and friends. This is needed as part of the effort to compile `libunwind` + `libc` without baremetal mode. This is part of #97191. This should still be spec compliant, since `dlopen` always returns `NULL` and `dlerror` always returns an error message. > If dlopen() fails for any reason, it returns NULL. > The function dlclose() returns 0 on success, and nonzero on error. > Since the value of the symbol could actually be NULL (so that a NULL return from dlsym() need not indicate an error), the correct way to test for an error is to call dlerror() to clear any old error conditions, then call dlsym(), and then call dlerror() again, saving its return value into a variable, and check whether this saved value is not NULL. See: - https://linux.die.net/man/3/dlopen
1 parent f4e6ddb commit b151c7e

File tree

14 files changed

+238
-0
lines changed

14 files changed

+238
-0
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ set(TARGET_LIBC_ENTRYPOINTS
1717
libc.src.ctype.tolower
1818
libc.src.ctype.toupper
1919

20+
# dlfcn.h entrypoints
21+
libc.src.dlfcn.dlclose
22+
libc.src.dlfcn.dlerror
23+
libc.src.dlfcn.dlopen
24+
libc.src.dlfcn.dlsym
25+
2026
# errno.h entrypoints
2127
libc.src.errno.errno
2228

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ set(TARGET_LIBC_ENTRYPOINTS
1717
libc.src.ctype.tolower
1818
libc.src.ctype.toupper
1919

20+
# dlfcn.h entrypoints
21+
libc.src.dlfcn.dlclose
22+
libc.src.dlfcn.dlerror
23+
libc.src.dlfcn.dlopen
24+
libc.src.dlfcn.dlsym
25+
2026
# errno.h entrypoints
2127
libc.src.errno.errno
2228

libc/docs/dev/undefined_behavior.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@ The C23 standard states that if the value of the ``rnd`` argument of the
8989
the value of a math rounding direction macro, the direction of rounding is
9090
unspecified. LLVM's libc chooses to use the ``FP_INT_TONEAREST`` rounding
9191
direction in this case.
92+
93+
Non-const Constant Return Values
94+
--------------------------------
95+
Some libc functions, like ``dlerror()``, return ``char *`` instead of ``const char *`` and then tell the caller they promise not to to modify this value. Any modification of this value is undefined behavior.

libc/spec/posix.td

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,40 @@ def POSIX : StandardSpec<"POSIX"> {
222222
[] // Functions
223223
>;
224224

225+
HeaderSpec DlFcn = HeaderSpec<
226+
"dlfcn.h",
227+
[
228+
Macro<"RTLD_LAZY">,
229+
Macro<"RTLD_NOW">,
230+
Macro<"RTLD_GLOBAL">,
231+
Macro<"RTLD_LOCAL">,
232+
],
233+
[], // Types
234+
[], // Enumerations
235+
[
236+
FunctionSpec<
237+
"dlclose",
238+
RetValSpec<IntType>,
239+
[ArgSpec<VoidPtr>]
240+
>,
241+
FunctionSpec<
242+
"dlerror",
243+
RetValSpec<CharPtr>,
244+
[]
245+
>,
246+
FunctionSpec<
247+
"dlopen",
248+
RetValSpec<VoidPtr>,
249+
[ArgSpec<ConstCharPtr>, ArgSpec<IntType>]
250+
>,
251+
FunctionSpec<
252+
"dlsym",
253+
RetValSpec<VoidPtr>,
254+
[ArgSpec<VoidRestrictedPtr>, ArgSpec<ConstCharRestrictedPtr>]
255+
>,
256+
]
257+
>;
258+
225259
HeaderSpec FCntl = HeaderSpec<
226260
"fcntl.h",
227261
[], // Macros
@@ -1690,6 +1724,7 @@ def POSIX : StandardSpec<"POSIX"> {
16901724
ArpaInet,
16911725
CType,
16921726
Dirent,
1727+
DlFcn,
16931728
Errno,
16941729
FCntl,
16951730
PThread,

libc/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_subdirectory(__support)
22

33
add_subdirectory(ctype)
4+
add_subdirectory(dlfcn)
45
add_subdirectory(errno)
56
add_subdirectory(fenv)
67
add_subdirectory(inttypes)

libc/src/dlfcn/CMakeLists.txt

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
add_entrypoint_object(
2+
dlclose
3+
SRCS
4+
dlclose.cpp
5+
HDRS
6+
dlclose.h
7+
)
8+
9+
add_entrypoint_object(
10+
dlerror
11+
SRCS
12+
dlerror.cpp
13+
HDRS
14+
dlerror.h
15+
DEPENDS
16+
libc.include.dlfcn
17+
libc.src.errno.errno
18+
)
19+
20+
add_entrypoint_object(
21+
dlopen
22+
SRCS
23+
dlopen.cpp
24+
HDRS
25+
dlopen.h
26+
DEPENDS
27+
libc.include.dlfcn
28+
libc.src.errno.errno
29+
)
30+
31+
add_entrypoint_object(
32+
dlsym
33+
SRCS
34+
dlsym.cpp
35+
HDRS
36+
dlsym.h
37+
DEPENDS
38+
libc.include.dlfcn
39+
libc.src.errno.errno
40+
)

libc/src/dlfcn/dlclose.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of dlclose -----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "dlclose.h"
10+
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97917
16+
LLVM_LIBC_FUNCTION(int, dlclose, (void *)) { return -1; }
17+
18+
} // namespace LIBC_NAMESPACE

libc/src/dlfcn/dlclose.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header of dlclose ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_DLFCN_DLCLOSE_H
10+
#define LLVM_LIBC_SRC_DLFCN_DLCLOSE_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
int dlclose(void *);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_DLFCN_DLCLOSE_H

libc/src/dlfcn/dlerror.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of delerror ----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "dlerror.h"
10+
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97918
16+
LLVM_LIBC_FUNCTION(char *, dlerror, ()) {
17+
return const_cast<char *>("unsupported");
18+
}
19+
20+
} // namespace LIBC_NAMESPACE

libc/src/dlfcn/dlerror.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header of dlerror ------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_DLFCN_DLERROR_H
10+
#define LLVM_LIBC_SRC_DLFCN_DLERROR_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
char *dlerror();
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_DLFCN_DLERROR_H

libc/src/dlfcn/dlopen.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of dlopen -----------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "dlopen.h"
10+
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97919
16+
LLVM_LIBC_FUNCTION(void *, dlopen, (const char *, int)) { return nullptr; }
17+
18+
} // namespace LIBC_NAMESPACE

libc/src/dlfcn/dlopen.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header of dlopen -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_DLFCN_DLOPEN_H
10+
#define LLVM_LIBC_SRC_DLFCN_DLOPEN_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
void *dlopen(const char *, int);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_DLFCN_DLOPEN_H

libc/src/dlfcn/dlsym.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation of dlsym ------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "dlsym.h"
10+
11+
#include "src/__support/common.h"
12+
13+
namespace LIBC_NAMESPACE {
14+
15+
// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97920
16+
LLVM_LIBC_FUNCTION(void *, dlsym, (void *, const char *)) { return nullptr; }
17+
18+
} // namespace LIBC_NAMESPACE

libc/src/dlfcn/dlsym.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header of dlsym --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_DLFCN_DLSYM_H
10+
#define LLVM_LIBC_SRC_DLFCN_DLSYM_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
void *dlsym(void *, const char *);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_DLFCN_DLSYM_H

0 commit comments

Comments
 (0)