Skip to content
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

[libc] Add dlfcn.h placeholder #97501

Merged
merged 3 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.ctype.tolower
libc.src.ctype.toupper

# dlfcn.h entrypoints
libc.src.dlfcn.dlclose
libc.src.dlfcn.dlerror
libc.src.dlfcn.dlopen
libc.src.dlfcn.dlsym

# errno.h entrypoints
libc.src.errno.errno

Expand Down
6 changes: 6 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.ctype.tolower
libc.src.ctype.toupper

# dlfcn.h entrypoints
libc.src.dlfcn.dlclose
libc.src.dlfcn.dlerror
libc.src.dlfcn.dlopen
libc.src.dlfcn.dlsym

# errno.h entrypoints
libc.src.errno.errno

Expand Down
4 changes: 4 additions & 0 deletions libc/docs/dev/undefined_behavior.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ The C23 standard states that if the value of the ``rnd`` argument of the
the value of a math rounding direction macro, the direction of rounding is
unspecified. LLVM's libc chooses to use the ``FP_INT_TONEAREST`` rounding
direction in this case.

Non-const Constant Return Values
--------------------------------
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.
35 changes: 35 additions & 0 deletions libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1690,6 +1724,7 @@ def POSIX : StandardSpec<"POSIX"> {
ArpaInet,
CType,
Dirent,
DlFcn,
Errno,
FCntl,
PThread,
Expand Down
1 change: 1 addition & 0 deletions libc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_subdirectory(__support)

add_subdirectory(ctype)
add_subdirectory(dlfcn)
add_subdirectory(errno)
add_subdirectory(fenv)
add_subdirectory(inttypes)
Expand Down
40 changes: 40 additions & 0 deletions libc/src/dlfcn/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
add_entrypoint_object(
dlclose
SRCS
dlclose.cpp
HDRS
dlclose.h
)

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
)
18 changes: 18 additions & 0 deletions libc/src/dlfcn/dlclose.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- 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 {

// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97917
LLVM_LIBC_FUNCTION(int, dlclose, (void *)) { return -1; }

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/dlfcn/dlclose.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions libc/src/dlfcn/dlerror.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- 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 {

// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97918
LLVM_LIBC_FUNCTION(char *, dlerror, ()) {
return const_cast<char *>("unsupported");
Comment on lines +16 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that const_cast is fine. Despite the non-const char * return type, POSIX does state that "The application shall not modify the string returned": https://pubs.opengroup.org/onlinepubs/9699919799/functions/dlerror.html.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thoughts… it looks like the caller is not supposed to touch this pointer so this seemed safe to me? If someone knows better lmk and I will change it.

}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/dlfcn/dlerror.h
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions libc/src/dlfcn/dlopen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- 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 {

// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97919
LLVM_LIBC_FUNCTION(void *, dlopen, (const char *, int)) { return nullptr; }

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/dlfcn/dlopen.h
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions libc/src/dlfcn/dlsym.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- 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 {

// TODO(@izaakschroeder): https://github.com/llvm/llvm-project/issues/97920
LLVM_LIBC_FUNCTION(void *, dlsym, (void *, const char *)) { return nullptr; }

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/dlfcn/dlsym.h
Original file line number Diff line number Diff line change
@@ -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
Loading