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 dl_iterate_phdr and dladdr #121179

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

RossComputerGuy
Copy link
Contributor

This only adds the functions so they resolve, this makes it possible to compile libunwind.

@llvmbot llvmbot added the libc label Dec 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 27, 2024

@llvm/pr-subscribers-libc

Author: Tristan Ross (RossComputerGuy)

Changes

This only adds the functions so they resolve, this makes it possible to compile libunwind.


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

15 Files Affected:

  • (modified) libc/config/linux/aarch64/entrypoints.txt (+4)
  • (modified) libc/hdrgen/yaml/dlfcn.yaml (+9-1)
  • (modified) libc/hdrgen/yaml/link.yaml (+11-2)
  • (modified) libc/include/CMakeLists.txt (+3)
  • (modified) libc/include/llvm-libc-types/CMakeLists.txt (+3)
  • (added) libc/include/llvm-libc-types/Dl_info.h (+19)
  • (added) libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h (+18)
  • (added) libc/include/llvm-libc-types/struct_dl_phdr_info.h (+26)
  • (modified) libc/src/CMakeLists.txt (+1)
  • (modified) libc/src/dlfcn/CMakeLists.txt (+10)
  • (added) libc/src/dlfcn/dladdr.cpp (+18)
  • (added) libc/src/dlfcn/dladdr.h (+21)
  • (added) libc/src/link/CMakeLists.txt (+11)
  • (added) libc/src/link/dl_iterate_phdr.cpp (+21)
  • (added) libc/src/link/dl_iterate_phdr.h (+21)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 00f0c6a8bfb8e4..ad71df4e6801d7 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -18,11 +18,15 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.ctype.toupper
 
     # dlfcn.h entrypoints
+    libc.src.dlfcn.dladdr
     libc.src.dlfcn.dlclose
     libc.src.dlfcn.dlerror
     libc.src.dlfcn.dlopen
     libc.src.dlfcn.dlsym
 
+    # link.h entrypoints
+    libc.src.link.dl_iterate_phdr
+
     # errno.h entrypoints
     libc.src.errno.errno
 
diff --git a/libc/hdrgen/yaml/dlfcn.yaml b/libc/hdrgen/yaml/dlfcn.yaml
index 725ee705714a75..56c37bf20b2f90 100644
--- a/libc/hdrgen/yaml/dlfcn.yaml
+++ b/libc/hdrgen/yaml/dlfcn.yaml
@@ -8,7 +8,8 @@ macros:
     macro_value: null
   - macro_name: RTLD_LOCAL
     macro_value: null
-types: []
+types:
+  - type_name: Dl_info
 enums: []
 objects: []
 functions:
@@ -37,3 +38,10 @@ functions:
     arguments:
       - type: void *__restrict
       - type: const char *__restrict
+  - name: dladdr
+    standards:
+      - GNUExtensions
+    return_type: int
+    arguments:
+      - type: const void *
+      - type: Dl_info *
diff --git a/libc/hdrgen/yaml/link.yaml b/libc/hdrgen/yaml/link.yaml
index d1963a86813af3..acc96eb8fc27b5 100644
--- a/libc/hdrgen/yaml/link.yaml
+++ b/libc/hdrgen/yaml/link.yaml
@@ -2,7 +2,16 @@ header: link.h
 standards:
   - Linux
 macros: []
-types: []
+types:
+  - type_name: struct_dl_phdr_info
+  - type_name: __dl_iterate_phdr_callback_t
 enums: []
 objects: []
-functions: []
+functions:
+  - name: dl_iterate_phdr
+    standards:
+      - Linux
+    return_type: int
+    arguments:
+      - type: __dl_iterate_phdr_callback_t
+      - type: void *
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 3a05c01abba5a4..21bb341664effe 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -75,6 +75,7 @@ add_header_macro(
   dlfcn.h
   DEPENDS
     .llvm-libc-macros.dlfcn_macros
+    .llvm-libc-types.Dl_info
     .llvm_libc_common_h
 )
 
@@ -444,6 +445,8 @@ add_header_macro(
   link.h
   DEPENDS
     .llvm_libc_common_h
+    .llvm-libc-types.__dl_iterate_phdr_callback_t
+    .llvm-libc-types.struct_dl_phdr_info
     .llvm-libc-macros.link_macros
 )
 
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index ee734eafce3620..6698516be189af 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -1,3 +1,4 @@
+add_header(Dl_info HDR Dl_info.h)
 add_header(off64_t HDR off64_t.h)
 add_header(size_t HDR size_t.h)
 add_header(ssize_t HDR ssize_t.h)
@@ -67,6 +68,8 @@ else()
 endif()
 add_header(stack_t HDR stack_t.h DEPENDS .size_t)
 add_header(suseconds_t HDR suseconds_t.h)
+add_header(struct_dl_phdr_info HDR struct_dl_phdr_info.h DEPENDS .size_t libc.include.llvm-libc-macros.link_macros)
+add_header(__dl_iterate_phdr_callback_t HDR __dl_iterate_phdr_callback_t.h DEPENDS .size_t .struct_dl_phdr_info)
 add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
 add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)
 add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t)
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 00000000000000..b082e30549a020
--- /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/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h b/libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h
new file mode 100644
index 00000000000000..2f9a14cf5706cf
--- /dev/null
+++ b/libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h
@@ -0,0 +1,18 @@
+//===-- Definition of type __dl_iterate_phdr_callback_t -------------------===//
+//
+// 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_ITERATE_PHDR_CALLBACK_T_H
+#define LLVM_LIBC_TYPES___DL_ITERATE_PHDR_CALLBACK_T_H
+
+#include "size_t.h"
+#include "struct_dl_phdr_info.h"
+
+typedef int (*__dl_iterate_phdr_callback_t)(struct dl_phdr_info *, size_t,
+                                            void *);
+
+#endif // LLVM_LIBC_TYPES___DL_ITERATE_PHDR_CALLBACK_T_H
diff --git a/libc/include/llvm-libc-types/struct_dl_phdr_info.h b/libc/include/llvm-libc-types/struct_dl_phdr_info.h
new file mode 100644
index 00000000000000..cc568a96fccead
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_dl_phdr_info.h
@@ -0,0 +1,26 @@
+//===-- Definition of type struct dl_phdr_info ----------------------------===//
+//
+// 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_STRUCT_DL_PHDR_INFO_H
+#define LLVM_LIBC_TYPES_STRUCT_DL_PHDR_INFO_H
+
+#include "size_t.h"
+#include "../llvm-libc-macros/link-macros.h"
+
+struct dl_phdr_info {
+  ElfW(Addr) dlpi_addr;
+  const char *dlpi_name;
+  const ElfW(Phdr) * dlpi_phdr;
+  ElfW(Half) dlpi_phnum;
+  unsigned long long dlpi_adds;
+  unsigned long long dlpi_subs;
+  size_t dlpi_tls_modid;
+  void *dlpi_tls_data;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_DL_PHDR_INFO_H
diff --git a/libc/src/CMakeLists.txt b/libc/src/CMakeLists.txt
index 9fc331ad18a391..268d977d28a8c2 100644
--- a/libc/src/CMakeLists.txt
+++ b/libc/src/CMakeLists.txt
@@ -6,6 +6,7 @@ add_subdirectory(dlfcn)
 add_subdirectory(errno)
 add_subdirectory(fenv)
 add_subdirectory(inttypes)
+add_subdirectory(link)
 add_subdirectory(math)
 add_subdirectory(stdbit)
 add_subdirectory(stdfix)
diff --git a/libc/src/dlfcn/CMakeLists.txt b/libc/src/dlfcn/CMakeLists.txt
index e3a51ba65764d4..205275a6825739 100644
--- a/libc/src/dlfcn/CMakeLists.txt
+++ b/libc/src/dlfcn/CMakeLists.txt
@@ -1,3 +1,13 @@
+add_entrypoint_object(
+  dladdr
+  SRCS
+    dladdr.cpp
+  HDRS
+    dladdr.h
+  DEPENDS
+    libc.include.dlfcn
+)
+
 add_entrypoint_object(
   dlclose
   SRCS
diff --git a/libc/src/dlfcn/dladdr.cpp b/libc/src/dlfcn/dladdr.cpp
new file mode 100644
index 00000000000000..824b4553120a2a
--- /dev/null
+++ b/libc/src/dlfcn/dladdr.cpp
@@ -0,0 +1,18 @@
+//===-- 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 {
+
+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 00000000000000..58b859ff91e7e8
--- /dev/null
+++ b/libc/src/dlfcn/dladdr.h
@@ -0,0 +1,21 @@
+//===-- 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 "include/llvm-libc-types/Dl_info.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
diff --git a/libc/src/link/CMakeLists.txt b/libc/src/link/CMakeLists.txt
new file mode 100644
index 00000000000000..ce64f0c1688423
--- /dev/null
+++ b/libc/src/link/CMakeLists.txt
@@ -0,0 +1,11 @@
+add_entrypoint_object(
+  dl_iterate_phdr
+  SRCS
+    dl_iterate_phdr.cpp
+  HDRS
+    dl_iterate_phdr.h
+  DEPENDS
+    libc.include.llvm-libc-types.__dl_iterate_phdr_callback_t
+    libc.include.llvm-libc-types.struct_dl_phdr_info
+    libc.include.llvm-libc-types.size_t
+)
diff --git a/libc/src/link/dl_iterate_phdr.cpp b/libc/src/link/dl_iterate_phdr.cpp
new file mode 100644
index 00000000000000..b8bceab07ce195
--- /dev/null
+++ b/libc/src/link/dl_iterate_phdr.cpp
@@ -0,0 +1,21 @@
+//===-- Implementation of dl_iterate_phdr ---------------------------------===//
+//
+// 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 "src/link/dl_iterate_phdr.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, dl_iterate_phdr, (__dl_iterate_phdr_callback_t callback, void* data)) {
+  (void)callback;
+  (void)data;
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/link/dl_iterate_phdr.h b/libc/src/link/dl_iterate_phdr.h
new file mode 100644
index 00000000000000..906adf182089b0
--- /dev/null
+++ b/libc/src/link/dl_iterate_phdr.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for dl_iterate_phdr ---------------*- 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_LINK_DL_ITERATE_PHDR_H
+#define LLVM_LIBC_SRC_LINK_DL_ITERATE_PHDR_H
+
+#include "src/__support/macros/config.h"
+#include "include/llvm-libc-types/__dl_iterate_phdr_callback_t.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int dl_iterate_phdr(__dl_iterate_phdr_callback_t callback, void* data);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_STRING_MEMCHR_H

Copy link

github-actions bot commented Dec 27, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@RossComputerGuy RossComputerGuy force-pushed the feat/libc-link branch 2 times, most recently from 0a3fbc2 to 6666623 Compare December 27, 2024 05:28
Copy link
Member

@thesamesam thesamesam left a comment

Choose a reason for hiding this comment

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

I'm not a LLVM libc maintainer, but I think adding stubs so that userland compiles without some rationale as to:
a) why it shouldn't be implemented properly;
b) why the userland can't be ported
would be nice to see.


namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(int, dl_iterate_phdr,
Copy link
Member

Choose a reason for hiding this comment

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

This looks a lot like musl's src/ldso/dl_iterate_phdr.c. If that's the case, please provide some attribution, and also mention it in the commit message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do

Choose a reason for hiding this comment

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

Simply mentioning it comes from musl isn't sufficient -- musl's COPYRIGHT file imposes very specific requirements, as does LLVM's own license file:

llvm-project/LICENSE.TXT

Lines 224 to 234 in 814902a

==============================================================================
Software from third parties included in the LLVM Project:
==============================================================================
The LLVM Project contains third party software which is under different license
terms. All such code will be identified clearly using at least one of two
mechanisms:
1) It will be in a separate directory tree with its own `LICENSE.txt` or
`LICENSE` file at the top containing the specific license and restrictions
which apply to that software, or
2) It will contain specific license and restriction terms at the top of every
file.

MIT has very few requirements but what it does require has not been satisfied by the most recent push.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fun, I've not really had to deal with licensing things like this before. I'm not sure what exactly to do.

@@ -0,0 +1,57 @@
//===-- Implementation of dl_iterate_phdr ---------------------------------===//
Copy link
Member

Choose a reason for hiding this comment

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

I thought that this PR was just to make libunwind work (per PR description), hence dladdr being a stub, but we have a real implementation here for dl_iterate_phdr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's because dl_iterate_phdr is simple enough to implement. dladdr will require more stuff like dlopen, dlclose, and dlsym to be implemented. Currently, there's TODO's for @izaakschroeder to work on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just found this #97928

dl_iterate_phdr implementation based on Musl's implementation.
@jhuber6
Copy link
Contributor

jhuber6 commented Dec 27, 2024

I'd assume we'd want our own implementation for this stuff. I've long adovated for just taking LLVM's ELF header and porting the few places it uses STL types to just use internal ones.

@RossComputerGuy
Copy link
Contributor Author

I'm not sure, the implementation I made is similar to Musl's but both are simple implementations. glibc's implementation is much larger but looks to achieve the same results.

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.

5 participants