Skip to content

[libc] Add dladdr to dlfcn.h #149872

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

Merged
merged 9 commits into from
Aug 5, 2025
Merged

[libc] Add dladdr to dlfcn.h #149872

merged 9 commits into from
Aug 5, 2025

Conversation

Caslyn
Copy link
Contributor

@Caslyn Caslyn commented Jul 21, 2025

A initial commit for #97929, this adds a stub implementation for dladdr and includes the definition for the DL_info type used as one of its arguments.

While the dladdr implementation relies on dynamic linker support, this patch will add its prototype in the generated dlfcn.h header so that it can be used by downstream platforms that have their own dladdr implementation.

Related to llvm#97929, this adds a stub implementation for `dladdr` and
includes the definition for the `DL_info` type used as one of its
arguments.

While the `dladdr` implementation relies on dynamic linker support, this
patch will add its prototype in the generated `dlfcn.h` header so that
it can be used by downstream platforms that have their own `dladdr`
implementation.
@Caslyn Caslyn self-assigned this Jul 21, 2025
@Caslyn Caslyn added the libc label Jul 21, 2025
@Caslyn Caslyn requested a review from frobtech July 21, 2025 19:01
@llvmbot
Copy link
Member

llvmbot commented Jul 21, 2025

@llvm/pr-subscribers-libc

Author: Caslyn Tonelli (Caslyn)

Changes

A initial commit for #97929, this adds a stub implementation for dladdr and includes the definition for the DL_info type used as one of its arguments.

While the dladdr implementation relies on dynamic linker support, this patch will add its prototype in the generated dlfcn.h header so that it can be used by downstream platforms that have their own dladdr implementation.


Full diff: https://github.com/llvm/llvm-project/pull/149872.diff

4 Files Affected:

  • (modified) libc/include/dlfcn.yaml (+9)
  • (added) libc/include/llvm-libc-types/DL_info.h (+19)
  • (added) libc/src/dlfcn/dladdr.cpp (+19)
  • (added) libc/src/dlfcn/dladdr.h (+20)
diff --git a/libc/include/dlfcn.yaml b/libc/include/dlfcn.yaml
index 28be34dbd95bd..ea4de6860e7ba 100644
--- a/libc/include/dlfcn.yaml
+++ b/libc/include/dlfcn.yaml
@@ -29,6 +29,8 @@ macros:
     standards:
       - gnu
     macro_value: "0x01000"
+types:
+  - type_name: DL_info
 functions:
   - name: dlclose
     standards:
@@ -55,3 +57,10 @@ functions:
     arguments:
       - type: void *__restrict
       - type: const char *__restrict
+  - name: dladdr
+    standards:
+      - POSIX
+    return_type: int
+    arguments:
+      - type: const void *
+      - type: DL_info *
diff --git a/libc/include/llvm-libc-types/DL_info.h b/libc/include/llvm-libc-types/DL_info.h
new file mode 100644
index 0000000000000..08dcba2c2945f
--- /dev/null
+++ b/libc/include/llvm-libc-types/DL_info.h
@@ -0,0 +1,19 @@
+//===-- Definition of DL_info type ----------------------------------------===//
+//
+// 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_TYPES_DL_INFO_H
+#define LLVM_LIBC_TYPES_DL_INFO_H
+
+typedef struct {
+  const char *dli_fname;
+  void *dli_fbase;
+  const char *dli_sname;
+  void *dli_saddr;
+} DL_info;
+
+#endif // LLVM_LIBC_TYPES_DL_INFO_H
diff --git a/libc/src/dlfcn/dladdr.cpp b/libc/src/dlfcn/dladdr.cpp
new file mode 100644
index 0000000000000..faaefcf161b54
--- /dev/null
+++ b/libc/src/dlfcn/dladdr.cpp
@@ -0,0 +1,19 @@
+//===-- Implementation of dladdr ------------------------------------------===//
+//
+// 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 "dladdr.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+// TODO: https:// github.com/llvm/llvm-project/issues/97929
+LLVM_LIBC_FUNCTION(int, dladdr, (const void *, DL_info *)) { return -1; }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/dlfcn/dladdr.h b/libc/src/dlfcn/dladdr.h
new file mode 100644
index 0000000000000..a5f38973b7c02
--- /dev/null
+++ b/libc/src/dlfcn/dladdr.h
@@ -0,0 +1,20 @@
+//===-- Implementation header of dladdr -------------------------*- 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_DLADDR_H
+#define LLVM_LIBC_SRC_DLFCN_DLADDR_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int dladdr(const void *, DL_info *);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_DLFCN_DLADDR_H

Copy link
Contributor

@frobtech frobtech left a comment

Choose a reason for hiding this comment

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

lgtm with the correct identifier names.

@Caslyn Caslyn merged commit 5a076e3 into llvm:main Aug 5, 2025
19 checks passed
@Caslyn Caslyn deleted the libc-dladdr branch August 5, 2025 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants