From eddd5a17090ad4559d270bda623af0ae884e475d Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Sat, 15 Mar 2025 00:56:01 -0700 Subject: [PATCH 1/2] [libc] Define (stub) dl_iterate_phdr This fleshes out the a little more, including the `struct dl_phdr_info` type and declaring the dl_iterate_phdr function. There is only a no-op implementation without tests, as for the existing dlfcn functions. --- libc/config/linux/aarch64/entrypoints.txt | 3 ++ libc/config/linux/riscv/entrypoints.txt | 3 ++ libc/config/linux/x86_64/entrypoints.txt | 3 ++ libc/hdr/types/CMakeLists.txt | 8 +++++ libc/hdr/types/struct_dl_phdr_info.h | 21 +++++++++++++ libc/include/CMakeLists.txt | 2 ++ libc/include/link.h.def | 17 ----------- libc/include/link.yaml | 25 +++++++++++----- libc/include/llvm-libc-types/CMakeLists.txt | 2 ++ .../__dl_iterate_phdr_callback_t.h | 19 ++++++++++++ .../llvm-libc-types/struct_dl_phdr_info.h | 30 +++++++++++++++++++ libc/src/CMakeLists.txt | 3 +- libc/src/link/dl_iterate_phdr.cpp | 25 ++++++++++++++++ libc/src/link/dl_iterate_phdr.h | 22 ++++++++++++++ libc/utils/hdrgen/hdrgen/header.py | 1 + 15 files changed, 159 insertions(+), 25 deletions(-) create mode 100644 libc/hdr/types/struct_dl_phdr_info.h delete mode 100644 libc/include/link.h.def create mode 100644 libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h create mode 100644 libc/include/llvm-libc-types/struct_dl_phdr_info.h create mode 100644 libc/src/link/dl_iterate_phdr.cpp create mode 100644 libc/src/link/dl_iterate_phdr.h diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt index ab1917259519b..6d42fda5b5f49 100644 --- a/libc/config/linux/aarch64/entrypoints.txt +++ b/libc/config/linux/aarch64/entrypoints.txt @@ -842,6 +842,9 @@ if(LLVM_LIBC_FULL_BUILD) libc.src.arpa.inet.ntohl libc.src.arpa.inet.ntohs + # link.h entrypoints + libc.src.link.dl_iterate_phdr + # pthread.h entrypoints libc.src.pthread.pthread_atfork libc.src.pthread.pthread_attr_destroy diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt index 4b69e43bce37d..75e845925e691 100644 --- a/libc/config/linux/riscv/entrypoints.txt +++ b/libc/config/linux/riscv/entrypoints.txt @@ -795,6 +795,9 @@ if(LLVM_LIBC_FULL_BUILD) libc.src.arpa.inet.ntohl libc.src.arpa.inet.ntohs + # link.h entrypoints + libc.src.link.dl_iterate_phdr + # pthread.h entrypoints libc.src.pthread.pthread_atfork libc.src.pthread.pthread_attr_destroy diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt index a29478898fe70..bb3c30157b0ce 100644 --- a/libc/config/linux/x86_64/entrypoints.txt +++ b/libc/config/linux/x86_64/entrypoints.txt @@ -960,6 +960,9 @@ if(LLVM_LIBC_FULL_BUILD) libc.src.arpa.inet.ntohl libc.src.arpa.inet.ntohs + # link.h entrypoints + libc.src.link.dl_iterate_phdr + # pthread.h entrypoints libc.src.pthread.pthread_atfork libc.src.pthread.pthread_attr_destroy diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt index 84a2647ba664d..5aa7b868c5e75 100644 --- a/libc/hdr/types/CMakeLists.txt +++ b/libc/hdr/types/CMakeLists.txt @@ -9,6 +9,14 @@ add_proxy_header_library( libc.include.stdlib ) +add_proxy_header_library( + struct_dl_phdr_info + HDRS + struct_dl_phdr_info.h + FULL_BUILD_DEPENDS + libc.include.llvm-libc-types.struct_dl_phdr_info +) + add_proxy_header_library( ldiv_t HDRS diff --git a/libc/hdr/types/struct_dl_phdr_info.h b/libc/hdr/types/struct_dl_phdr_info.h new file mode 100644 index 0000000000000..0cfb3c1309bde --- /dev/null +++ b/libc/hdr/types/struct_dl_phdr_info.h @@ -0,0 +1,21 @@ +//===-- Proxy for 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_HDR_TYPES_STRUCT_DL_PHDR_INFO_H +#define LLVM_LIBC_HDR_TYPES_STRUCT_DL_PHDR_INFO_H + +#ifdef LIBC_FULL_BUILD + +#include "include/llvm-libc-types/struct_dl_phdr_info.h" + +#else + +#include + +#endif // LIBC_FULL_BUILD + +#endif // LLVM_LIBC_HDR_TYPES_STRUCT_DL_PHDR_INFO_H diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt index 34f7c78b9783e..409737762ac41 100644 --- a/libc/include/CMakeLists.txt +++ b/libc/include/CMakeLists.txt @@ -431,6 +431,8 @@ add_header_macro( DEPENDS .llvm_libc_common_h .llvm-libc-macros.link_macros + .llvm-libc-types.struct_dl_phdr_info + .llvm-libc-types.__dl_iterate_phdr_callback_t ) add_header_macro( diff --git a/libc/include/link.h.def b/libc/include/link.h.def deleted file mode 100644 index ebab81c841b8d..0000000000000 --- a/libc/include/link.h.def +++ /dev/null @@ -1,17 +0,0 @@ -//===-- GNU header link.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_LINK_H -#define LLVM_LIBC_LINK_H - -#include "__llvm-libc-common.h" -#include "llvm-libc-macros/link-macros.h" - -%%public_api() - -#endif // LLVM_LIBC_LINK_H diff --git a/libc/include/link.yaml b/libc/include/link.yaml index 1cd609e292b53..7e303a4436d83 100644 --- a/libc/include/link.yaml +++ b/libc/include/link.yaml @@ -1,9 +1,20 @@ header: link.h -header_template: link.h.def standards: - - Linux -macros: [] -types: [] -enums: [] -objects: [] -functions: [] + - svid +macros: + - macro_name: ElfW + standards: + - gnu + macro_header: link-macros.h +types: + - type_name: struct_dl_phdr_info + standards: + - gnu +functions: + - name: dl_iterate_phdr + standards: + - gnu + return_type: int + arguments: + - type: __dl_iterate_phdr_callback_t + - type: void * diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt index 58761ac97d7cf..bf8bdfe89943c 100644 --- a/libc/include/llvm-libc-types/CMakeLists.txt +++ b/libc/include/llvm-libc-types/CMakeLists.txt @@ -5,6 +5,7 @@ add_header(__atfork_callback_t HDR __atfork_callback_t.h) add_header(__bsearchcompare_t HDR __bsearchcompare_t.h) add_header(__lsearchcompare_t HDR __lsearchcompare_t.h) add_header(__call_once_func_t HDR __call_once_func_t.h) +add_header(__dl_iterate_phdr_callback_t HDR __dl_iterate_phdr_callback_t.h DEPENDS .size_t) add_header(__exec_argv_t HDR __exec_argv_t.h) add_header(__exec_envp_t HDR __exec_envp_t.h) add_header(__futex_word HDR __futex_word.h) @@ -69,6 +70,7 @@ add_header(sighandler_t HDR sighandler_t.h) add_header(stack_t HDR stack_t.h DEPENDS .size_t) add_header(suseconds_t HDR suseconds_t.h) add_header(struct_dirent HDR struct_dirent.h DEPENDS .ino_t .off_t) +add_header(struct_dl_phdr_info HDR struct_dl_phdr_info.h DEPENDS .size_t libc.include.llvm-libc-macros.link_macros) add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t) 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) 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 0000000000000..52078daf7d3ce --- /dev/null +++ b/libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h @@ -0,0 +1,19 @@ +//===-- 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" + +struct dl_phdr_info; + +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 0000000000000..2b9a5d21b4a13 --- /dev/null +++ b/libc/include/llvm-libc-types/struct_dl_phdr_info.h @@ -0,0 +1,30 @@ +//===-- 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 "../llvm-libc-macros/link-macros.h" +#include "size_t.h" +#include +#include + +struct dl_phdr_info { + ElfW(Addr) dlpi_addr; + const char *dlpi_name; + const ElfW(Phdr) * dlpi_phdr; + ElfW(Half) dlpi_phnum; + + uint64_t dlpi_adds; + uint64_t 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 19a354ceee4b6..a665253c4cc03 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) @@ -13,9 +14,9 @@ add_subdirectory(stdio) add_subdirectory(stdlib) add_subdirectory(string) add_subdirectory(strings) -add_subdirectory(wchar) add_subdirectory(time) add_subdirectory(unistd) +add_subdirectory(wchar) if(${LIBC_TARGET_OS} STREQUAL "linux") add_subdirectory(dirent) diff --git a/libc/src/link/dl_iterate_phdr.cpp b/libc/src/link/dl_iterate_phdr.cpp new file mode 100644 index 0000000000000..7964411598d4a --- /dev/null +++ b/libc/src/link/dl_iterate_phdr.cpp @@ -0,0 +1,25 @@ +//===-- 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 "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 *arg)) { + // FIXME: For pure static linking, this can report just the executable with + // info from __ehdr_start or AT_{PHDR,PHNUM} decoding, and its PT_TLS; and it + // could report the vDSO. + (void)callback, (void)arg; + 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 0000000000000..2d75e7122bc3e --- /dev/null +++ b/libc/src/link/dl_iterate_phdr.h @@ -0,0 +1,22 @@ +//===-- Implementation header of 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_DLFCN_DL_ITERATE_PHDR_H +#define LLVM_LIBC_SRC_DLFCN_DL_ITERATE_PHDR_H + +#include "hdr/types/struct_dl_phdr_info.h" +#include "include/llvm-libc-types/__dl_iterate_phdr_callback_t.h" +#include "src/__support/macros/config.h" + +namespace LIBC_NAMESPACE_DECL { + +int dl_iterate_phdr(__dl_iterate_phdr_callback_t, void *); + +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC_DLFCN_DL_ITERATE_PHDR_H diff --git a/libc/utils/hdrgen/hdrgen/header.py b/libc/utils/hdrgen/hdrgen/header.py index 78444ed377be1..b054ed4e03bd0 100644 --- a/libc/utils/hdrgen/hdrgen/header.py +++ b/libc/utils/hdrgen/hdrgen/header.py @@ -44,6 +44,7 @@ "gnu": "GNU", "linux": "Linux", "uefi": "UEFI", + "svid": "SVID", } HEADER_TEMPLATE = """\ From 017df3e3fb622c4bf508afce2e32cb0d2823480b Mon Sep 17 00:00:00 2001 From: Roland McGrath Date: Sat, 15 Mar 2025 01:37:22 -0700 Subject: [PATCH 2/2] missing file --- libc/src/link/CMakeLists.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 libc/src/link/CMakeLists.txt diff --git a/libc/src/link/CMakeLists.txt b/libc/src/link/CMakeLists.txt new file mode 100644 index 0000000000000..f68950e625dc2 --- /dev/null +++ b/libc/src/link/CMakeLists.txt @@ -0,0 +1,7 @@ +add_entrypoint_object( + dl_iterate_phdr + SRCS + dl_iterate_phdr.cpp + HDRS + dl_iterate_phdr.h +)