[libc-commits] [libc] [libc][startup]: create header for ElfW and use in startup (PR #96510)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Mon Jun 24 09:14:05 PDT 2024


https://github.com/nickdesaulniers updated https://github.com/llvm/llvm-project/pull/96510

>From b2a1b95150e8fbca7df164e3a813c7876199011b Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 24 Jun 2024 08:47:03 -0700
Subject: [PATCH 1/2] [libc][startup]: create header for ElfW and use in
 startup

This is necessary for 32b platforms such as ARM and i386.

Link: #94128
---
 libc/include/llvm-libc-macros/link-macros.h | 13 +++++++++++++
 libc/startup/linux/do_start.cpp             |  9 +++++----
 2 files changed, 18 insertions(+), 4 deletions(-)
 create mode 100644 libc/include/llvm-libc-macros/link-macros.h

diff --git a/libc/include/llvm-libc-macros/link-macros.h b/libc/include/llvm-libc-macros/link-macros.h
new file mode 100644
index 0000000000000..5c8cadab8e71c
--- /dev/null
+++ b/libc/include/llvm-libc-macros/link-macros.h
@@ -0,0 +1,13 @@
+//===-- Definition of macros to for extra dynamic linker functionality ----===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifdef __LP64__
+#define ElfW(type) Elf64_ ## type
+#else
+#define ElfW(type) Elf32_ ## type
+#endif
diff --git a/libc/startup/linux/do_start.cpp b/libc/startup/linux/do_start.cpp
index 55fd575f7ad0b..3d7d32aead4fc 100644
--- a/libc/startup/linux/do_start.cpp
+++ b/libc/startup/linux/do_start.cpp
@@ -6,6 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 #include "startup/linux/do_start.h"
+#include "include/llvm-libc-macros/link-macros.h"
 #include "src/__support/OSUtil/syscall.h"
 #include "src/__support/threads/thread.h"
 #include "src/stdlib/atexit.h"
@@ -79,13 +80,13 @@ static ThreadAttributes main_thread_attrib;
 
   // After the env array, is the aux-vector. The end of the aux-vector is
   // denoted by an AT_NULL entry.
-  Elf64_Phdr *program_hdr_table = nullptr;
+  ElfW(Phdr) *program_hdr_table = nullptr;
   uintptr_t program_hdr_count = 0;
   app.auxv_ptr = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
   for (auto *aux_entry = app.auxv_ptr; aux_entry->id != AT_NULL; ++aux_entry) {
     switch (aux_entry->id) {
     case AT_PHDR:
-      program_hdr_table = reinterpret_cast<Elf64_Phdr *>(aux_entry->value);
+      program_hdr_table = reinterpret_cast<ElfW(Phdr) *>(aux_entry->value);
       break;
     case AT_PHNUM:
       program_hdr_count = aux_entry->value;
@@ -100,10 +101,10 @@ static ThreadAttributes main_thread_attrib;
 
   ptrdiff_t base = 0;
   app.tls.size = 0;
-  Elf64_Phdr *tls_phdr = nullptr;
+  ElfW(Phdr) *tls_phdr = nullptr;
 
   for (uintptr_t i = 0; i < program_hdr_count; ++i) {
-    Elf64_Phdr &phdr = program_hdr_table[i];
+    ElfW(Phdr) &phdr = program_hdr_table[i];
     if (phdr.p_type == PT_PHDR)
       base = reinterpret_cast<ptrdiff_t>(program_hdr_table) - phdr.p_vaddr;
     if (phdr.p_type == PT_DYNAMIC && _DYNAMIC)

>From c7af48463775a5b80224a92602a7b4fbab4313f6 Mon Sep 17 00:00:00 2001
From: Nick Desaulniers <ndesaulniers at google.com>
Date: Mon, 24 Jun 2024 09:13:53 -0700
Subject: [PATCH 2/2] cmake updates

---
 libc/include/llvm-libc-macros/CMakeLists.txt | 6 ++++++
 libc/startup/linux/CMakeLists.txt            | 1 +
 2 files changed, 7 insertions(+)

diff --git a/libc/include/llvm-libc-macros/CMakeLists.txt b/libc/include/llvm-libc-macros/CMakeLists.txt
index a4c2ca0df74d3..f6af11abd4dd7 100644
--- a/libc/include/llvm-libc-macros/CMakeLists.txt
+++ b/libc/include/llvm-libc-macros/CMakeLists.txt
@@ -103,6 +103,12 @@ add_macro_header(
     limits-macros.h
 )
 
+add_macro_header(
+  link_macros
+  HDR
+    link-macros.h
+)
+
 add_macro_header(
   math_macros
   HDR
diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index f041a4826dc94..336c5d0f6bfa2 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -98,6 +98,7 @@ add_object_library(
     libc.config.linux.app_h
     libc.include.sys_mman
     libc.include.sys_syscall
+    libc.include.llvm-libc-macros.link_macros
     libc.src.__support.threads.thread
     libc.src.__support.OSUtil.osutil
     libc.src.stdlib.exit



More information about the libc-commits mailing list