[libc-commits] [libc] [libc] Reapply ELF/LINK header changes (PR #102765)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Sat Aug 10 10:49:32 PDT 2024


https://github.com/SchrodingerZhu created https://github.com/llvm/llvm-project/pull/102765

This is based on @izaakschroeder  previous patch but I only select macro definitions for now. We need these definitions for VDSO work, which has been delayed for a very long time.

Co-Authored-by: Izaak Schroeder <izaak.schroeder at gmail.com>

>From a13b8f22bc09b7119fa18aa95e73d88018f5e5f7 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Sat, 10 Aug 2024 10:29:04 -0700
Subject: [PATCH 1/2] Reapply "[libc] Add `link.h` and `elf.h` headers"
 (#97931)

This reverts commit 1acb08660a7a2cd3f0f2e8f4fbfe34c830484480.
---
 libc/config/linux/aarch64/headers.txt         |  2 ++
 libc/config/linux/x86_64/headers.txt          |  2 ++
 libc/include/CMakeLists.txt                   | 20 ++++++++++++++
 libc/include/llvm-libc-macros/CMakeLists.txt  |  6 +++++
 libc/include/llvm-libc-macros/elf-macros.h    | 18 +++++++++++++
 libc/include/llvm-libc-macros/link-macros.h   | 26 +++++++++++++++++--
 libc/include/llvm-libc-types/CMakeLists.txt   | 16 ++++++++++++
 libc/include/llvm-libc-types/Dl_info.h        | 19 ++++++++++++++
 .../__dl_iterate_phdr_callback_t.h            | 17 ++++++++++++
 .../llvm-libc-types/struct_dl_phdr_info.h     | 26 +++++++++++++++++++
 10 files changed, 150 insertions(+), 2 deletions(-)
 create mode 100644 libc/include/llvm-libc-macros/elf-macros.h
 create mode 100644 libc/include/llvm-libc-types/Dl_info.h
 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

diff --git a/libc/config/linux/aarch64/headers.txt b/libc/config/linux/aarch64/headers.txt
index 8f898f0150905a..ebe053af99d803 100644
--- a/libc/config/linux/aarch64/headers.txt
+++ b/libc/config/linux/aarch64/headers.txt
@@ -2,6 +2,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.assert
     libc.include.ctype
     libc.include.dlfcn
+    libc.include.elf
     libc.include.errno
     libc.include.features
     libc.include.fenv
@@ -9,6 +10,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.stdint
     libc.include.inttypes
     libc.include.limits
+    libc.include.link
     libc.include.math
     libc.include.pthread
     libc.include.signal
diff --git a/libc/config/linux/x86_64/headers.txt b/libc/config/linux/x86_64/headers.txt
index 0294f62bc2f7a0..77e454e64395df 100644
--- a/libc/config/linux/x86_64/headers.txt
+++ b/libc/config/linux/x86_64/headers.txt
@@ -3,6 +3,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.ctype
     libc.include.dirent
     libc.include.dlfcn
+    libc.include.elf
     libc.include.errno
     libc.include.fcntl
     libc.include.features
@@ -11,6 +12,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.stdint
     libc.include.inttypes
     libc.include.limits
+    libc.include.link
     libc.include.math
     libc.include.pthread
     libc.include.sched
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index cbde24e17619f6..cba9954138dd25 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -81,6 +81,7 @@ add_header_macro(
   dlfcn.h.def
   dlfcn.h
   DEPENDS
+    .llvm-libc-types.Dl_info
     .llvm-libc-macros.dlfcn_macros
     .llvm_libc_common_h
 )
@@ -420,6 +421,25 @@ add_header_macro(
     .llvm-libc-types.posix_spawn_file_actions_t
 )
 
+add_gen_header(
+  link
+  DEF_FILE link.h.def
+  GEN_HDR link.h
+  DEPENDS
+    .llvm_libc_common_h
+    .llvm-libc-types.struct_dl_phdr_info
+    .llvm-libc-types.__dl_iterate_phdr_callback_t
+    .llvm-libc-macros.link_macros
+)
+
+add_gen_header(
+  elf
+  DEF_FILE elf.h.def
+  GEN_HDR elf.h
+  DEPENDS
+    .llvm-libc-macros.elf_macros
+)
+
 # TODO: Not all platforms will have a include/sys directory. Add the sys
 # directory and the targets for sys/*.h files conditional to the OS requiring
 # them.
diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index 3c10abef8768c0..60a8725f9ef63f 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -289,3 +289,9 @@ add_macro_header(
   HDR
     dlfcn-macros.h
 )
+
+add_macro_header(
+  elf_macros
+  HDR
+    elf-macros.h
+)
diff --git a/libc/include/llvm-libc-macros/elf-macros.h b/libc/include/llvm-libc-macros/elf-macros.h
new file mode 100644
index 00000000000000..fa4442abf0f5ce
--- /dev/null
+++ b/libc/include/llvm-libc-macros/elf-macros.h
@@ -0,0 +1,18 @@
+//===-- Definition of macros from elf.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_MACROS_ELF_MACROS_H
+#define LLVM_LIBC_MACROS_ELF_MACROS_H
+
+#if __has_include(<linux/elf.h>)
+#include <linux/elf.h>
+#else
+#error "cannot use <sys/elf.h> without proper system headers."
+#endif
+
+#endif // LLVM_LIBC_MACROS_ELF_MACROS_H
diff --git a/libc/include/llvm-libc-macros/link-macros.h b/libc/include/llvm-libc-macros/link-macros.h
index 5c8cadab8e71cb..f7461d9527a47b 100644
--- a/libc/include/llvm-libc-macros/link-macros.h
+++ b/libc/include/llvm-libc-macros/link-macros.h
@@ -6,8 +6,30 @@
 //
 //===----------------------------------------------------------------------===//
 
+#ifndef LLVM_LIBC_MACROS_LINK_MACROS_H
+#define LLVM_LIBC_MACROS_LINK_MACROS_H
+
+#include "elf-macros.h"
+
 #ifdef __LP64__
-#define ElfW(type) Elf64_ ## type
+#define ElfW(type) Elf64_##type
 #else
-#define ElfW(type) Elf32_ ## type
+#define ElfW(type) Elf32_##type
+#endif
+
+struct link_map {
+  ElfW(Addr) l_addr;
+  char *l_name;
+  ElfW(Dyn) * l_ld;
+  struct link_map *l_next, *l_prev;
+};
+
+struct r_debug {
+  int r_version;
+  struct link_map *r_map;
+  ElfW(Addr) r_brk;
+  enum { RT_CONSISTENT, RT_ADD, RT_DELETE } r_state;
+  ElfW(Addr) r_ldbase;
+};
+
 #endif
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 0fa86e0152f9ba..2a63a666c825bc 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -94,6 +94,22 @@ add_header(thrd_t HDR thrd_t.h DEPENDS .__thread_type)
 add_header(tss_t HDR tss_t.h)
 add_header(tss_dtor_t HDR tss_dtor_t.h)
 add_header(__atexithandler_t HDR __atexithandler_t.h)
+add_header(Dl_info HDR Dl_info.h)
+add_header(
+  __dl_iterate_phdr_callback_t 
+  HDR __dl_iterate_phdr_callback_t.h
+  DEPENDS
+    .size_t
+)
+add_header(
+  struct_dl_phdr_info 
+  HDR struct_dl_phdr_info.h
+  DEPENDS
+    .__dl_iterate_phdr_callback_t
+    .size_t
+    libc.include.llvm-libc-macros.link_macros
+)
+
 add_header(speed_t HDR speed_t.h)
 add_header(tcflag_t HDR tcflag_t.h)
 add_header(struct_termios HDR struct_termios.h DEPENDS .cc_t .speed_t .tcflag_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..613976a54bd612
--- /dev/null
+++ b/libc/include/llvm-libc-types/Dl_info.h
@@ -0,0 +1,19 @@
+//===-- Definition of type Dl_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_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..9d73e88076e00c
--- /dev/null
+++ b/libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h
@@ -0,0 +1,17 @@
+//===-- Definition of __dl_iterate_phdr_callback_t 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_ITERATE_PHDR_CALLBACK_T_H
+#define LLVM_LIBC_TYPES___DL_ITERATE_PHDR_CALLBACK_T_H
+
+#include "llvm-libc-types/size_t.h"
+
+typedef int (*__dl_iterate_phdr_callback_t)(struct dl_phdr_info *info,
+                                            size_t size, void *data);
+
+#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..66512b368e1904
--- /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 "llvm-libc-macros/link-macros.h"
+#include "llvm-libc-types/size_t.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 int dlpi_adds;
+  unsigned long long int dlpi_subs;
+  size_t dlpi_tls_modid;
+  void *dlpi_tls_data;
+};
+
+#endif // LLVM_LIBC_TYPES_STRUCT_DL_PHDR_INFO_H

>From 6b6732d0cb3eccb19589923a72a7e04bbd0647f7 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <i at zhuyi.fan>
Date: Sat, 10 Aug 2024 10:44:35 -0700
Subject: [PATCH 2/2] [libc] remove type definitions for now

---
 libc/include/CMakeLists.txt                   |  3 ---
 .../{llvm-libc-types/Dl_info.h => elf.h.def}  | 18 ++++++-------
 libc/include/link.h.def                       | 17 ++++++++++++
 libc/include/llvm-libc-macros/link-macros.h   | 15 -----------
 libc/include/llvm-libc-types/CMakeLists.txt   | 16 ------------
 .../__dl_iterate_phdr_callback_t.h            | 17 ------------
 .../llvm-libc-types/struct_dl_phdr_info.h     | 26 -------------------
 libc/newhdrgen/yaml/elf.yaml                  |  8 ++++++
 libc/newhdrgen/yaml/link.yaml                 |  8 ++++++
 9 files changed, 41 insertions(+), 87 deletions(-)
 rename libc/include/{llvm-libc-types/Dl_info.h => elf.h.def} (52%)
 create mode 100644 libc/include/link.h.def
 delete mode 100644 libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h
 delete mode 100644 libc/include/llvm-libc-types/struct_dl_phdr_info.h
 create mode 100644 libc/newhdrgen/yaml/elf.yaml
 create mode 100644 libc/newhdrgen/yaml/link.yaml

diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index cba9954138dd25..2b6eb61782a632 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -81,7 +81,6 @@ add_header_macro(
   dlfcn.h.def
   dlfcn.h
   DEPENDS
-    .llvm-libc-types.Dl_info
     .llvm-libc-macros.dlfcn_macros
     .llvm_libc_common_h
 )
@@ -427,8 +426,6 @@ add_gen_header(
   GEN_HDR link.h
   DEPENDS
     .llvm_libc_common_h
-    .llvm-libc-types.struct_dl_phdr_info
-    .llvm-libc-types.__dl_iterate_phdr_callback_t
     .llvm-libc-macros.link_macros
 )
 
diff --git a/libc/include/llvm-libc-types/Dl_info.h b/libc/include/elf.h.def
similarity index 52%
rename from libc/include/llvm-libc-types/Dl_info.h
rename to libc/include/elf.h.def
index 613976a54bd612..b9b2604fc7167b 100644
--- a/libc/include/llvm-libc-types/Dl_info.h
+++ b/libc/include/elf.h.def
@@ -1,4 +1,4 @@
-//===-- Definition of type Dl_info ----------------------------------------===//
+//===-- System V header elf.h ---------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,14 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_LIBC_TYPES_DL_INFO_H
-#define LLVM_LIBC_TYPES_DL_INFO_H
+#ifndef LLVM_LIBC_ELF_H
+#define LLVM_LIBC_ELF_H
 
-typedef struct {
-  const char *dli_fname;
-  void *dli_fbase;
-  const char *dli_sname;
-  void *dli_saddr;
-} Dl_info;
+#include "__llvm-libc-common.h"
+#include "llvm-libc-macros/elf-macros.h"
 
-#endif // LLVM_LIBC_TYPES_DL_INFO_H
+%%public_api()
+
+#endif // LLVM_LIBC_ELF_H
diff --git a/libc/include/link.h.def b/libc/include/link.h.def
new file mode 100644
index 00000000000000..ebab81c841b8dd
--- /dev/null
+++ b/libc/include/link.h.def
@@ -0,0 +1,17 @@
+//===-- 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/llvm-libc-macros/link-macros.h b/libc/include/llvm-libc-macros/link-macros.h
index f7461d9527a47b..89e7bb50aa5566 100644
--- a/libc/include/llvm-libc-macros/link-macros.h
+++ b/libc/include/llvm-libc-macros/link-macros.h
@@ -17,19 +17,4 @@
 #define ElfW(type) Elf32_##type
 #endif
 
-struct link_map {
-  ElfW(Addr) l_addr;
-  char *l_name;
-  ElfW(Dyn) * l_ld;
-  struct link_map *l_next, *l_prev;
-};
-
-struct r_debug {
-  int r_version;
-  struct link_map *r_map;
-  ElfW(Addr) r_brk;
-  enum { RT_CONSISTENT, RT_ADD, RT_DELETE } r_state;
-  ElfW(Addr) r_ldbase;
-};
-
 #endif
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 2a63a666c825bc..0fa86e0152f9ba 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -94,22 +94,6 @@ add_header(thrd_t HDR thrd_t.h DEPENDS .__thread_type)
 add_header(tss_t HDR tss_t.h)
 add_header(tss_dtor_t HDR tss_dtor_t.h)
 add_header(__atexithandler_t HDR __atexithandler_t.h)
-add_header(Dl_info HDR Dl_info.h)
-add_header(
-  __dl_iterate_phdr_callback_t 
-  HDR __dl_iterate_phdr_callback_t.h
-  DEPENDS
-    .size_t
-)
-add_header(
-  struct_dl_phdr_info 
-  HDR struct_dl_phdr_info.h
-  DEPENDS
-    .__dl_iterate_phdr_callback_t
-    .size_t
-    libc.include.llvm-libc-macros.link_macros
-)
-
 add_header(speed_t HDR speed_t.h)
 add_header(tcflag_t HDR tcflag_t.h)
 add_header(struct_termios HDR struct_termios.h DEPENDS .cc_t .speed_t .tcflag_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
deleted file mode 100644
index 9d73e88076e00c..00000000000000
--- a/libc/include/llvm-libc-types/__dl_iterate_phdr_callback_t.h
+++ /dev/null
@@ -1,17 +0,0 @@
-//===-- Definition of __dl_iterate_phdr_callback_t 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_ITERATE_PHDR_CALLBACK_T_H
-#define LLVM_LIBC_TYPES___DL_ITERATE_PHDR_CALLBACK_T_H
-
-#include "llvm-libc-types/size_t.h"
-
-typedef int (*__dl_iterate_phdr_callback_t)(struct dl_phdr_info *info,
-                                            size_t size, void *data);
-
-#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
deleted file mode 100644
index 66512b368e1904..00000000000000
--- a/libc/include/llvm-libc-types/struct_dl_phdr_info.h
+++ /dev/null
@@ -1,26 +0,0 @@
-//===-- 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 "llvm-libc-types/size_t.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 int dlpi_adds;
-  unsigned long long int dlpi_subs;
-  size_t dlpi_tls_modid;
-  void *dlpi_tls_data;
-};
-
-#endif // LLVM_LIBC_TYPES_STRUCT_DL_PHDR_INFO_H
diff --git a/libc/newhdrgen/yaml/elf.yaml b/libc/newhdrgen/yaml/elf.yaml
new file mode 100644
index 00000000000000..2e9db329e22979
--- /dev/null
+++ b/libc/newhdrgen/yaml/elf.yaml
@@ -0,0 +1,8 @@
+header: elf.h
+standards:
+  - Linux
+macros: []
+types: []
+enums: []
+objects: []
+functions: []
diff --git a/libc/newhdrgen/yaml/link.yaml b/libc/newhdrgen/yaml/link.yaml
new file mode 100644
index 00000000000000..d1963a86813af3
--- /dev/null
+++ b/libc/newhdrgen/yaml/link.yaml
@@ -0,0 +1,8 @@
+header: link.h
+standards:
+  - Linux
+macros: []
+types: []
+enums: []
+objects: []
+functions: []



More information about the libc-commits mailing list