[libc-commits] [libc] [libc] expose aux vector (PR #75806)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Mon Dec 18 07:00:56 PST 2023


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

This patch list aux vector related definitions to app.h. Because startup's refactoring is in progress, this patch still contains duplicated changes. This problem will be addressed very soon in an incoming patch.

>From 496f4292697a1b6383b53ca20f722108d5bfe832 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Mon, 18 Dec 2023 09:57:23 -0500
Subject: [PATCH] [libc] expose aux vector

This patch list aux vector related definitions to app.h. Because
startup's refactoring is in progress, this patch still contains
duplicated changes. This problem will be addressed very soon in
an incoming patch.
---
 libc/config/linux/app.h              | 14 +++++++++++++-
 libc/startup/linux/aarch64/start.cpp | 13 ++++---------
 libc/startup/linux/riscv/start.cpp   | 13 ++++---------
 libc/startup/linux/x86_64/start.cpp  | 13 ++++---------
 4 files changed, 25 insertions(+), 28 deletions(-)

diff --git a/libc/config/linux/app.h b/libc/config/linux/app.h
index 0d2f9475c10db6..5033d5b79b057c 100644
--- a/libc/config/linux/app.h
+++ b/libc/config/linux/app.h
@@ -49,11 +49,20 @@ typedef uintptr_t ArgcType;
 typedef uintptr_t ArgVEntryType;
 
 typedef uintptr_t EnvironType;
-typedef uintptr_t AuxEntryType;
 #else
 #error "argc and argv types are not defined for the target platform."
 #endif
 
+// Linux manpage on `proc(5)` says that the aux vector is an array of
+// unsigned long pairs.
+// (see: https://man7.org/linux/man-pages/man5/proc.5.html)
+using AuxEntryType = unsigned long;
+// Using the naming convention from `proc(5)`.
+struct AuxEntry {
+  AuxEntryType id;
+  AuxEntryType value;
+};
+
 struct Args {
   ArgcType argc;
 
@@ -78,6 +87,9 @@ struct AppProperties {
 
   // Environment data.
   EnvironType *env_ptr;
+
+  // Auxiliary vector data.
+  AuxEntry *auxv_ptr;
 };
 
 extern AppProperties app;
diff --git a/libc/startup/linux/aarch64/start.cpp b/libc/startup/linux/aarch64/start.cpp
index c3e20eb09e4b42..bc01582aeb49c7 100644
--- a/libc/startup/linux/aarch64/start.cpp
+++ b/libc/startup/linux/aarch64/start.cpp
@@ -126,12 +126,7 @@ static void call_fini_array_callbacks() {
 } // namespace LIBC_NAMESPACE
 
 using LIBC_NAMESPACE::app;
-
-// TODO: Would be nice to use the aux entry structure from elf.h when available.
-struct AuxEntry {
-  uint64_t type;
-  uint64_t value;
-};
+using LIBC_NAMESPACE::AuxEntry;
 
 __attribute__((noinline)) static void do_start() {
   auto tid = LIBC_NAMESPACE::syscall_impl<long>(SYS_gettid);
@@ -155,9 +150,9 @@ __attribute__((noinline)) static void do_start() {
   // denoted by an AT_NULL entry.
   Elf64_Phdr *program_hdr_table = nullptr;
   uintptr_t program_hdr_count;
-  for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
-       aux_entry->type != AT_NULL; ++aux_entry) {
-    switch (aux_entry->type) {
+  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);
       break;
diff --git a/libc/startup/linux/riscv/start.cpp b/libc/startup/linux/riscv/start.cpp
index 4d37662ccea13c..5b6e5bde8da81d 100644
--- a/libc/startup/linux/riscv/start.cpp
+++ b/libc/startup/linux/riscv/start.cpp
@@ -115,12 +115,7 @@ static void call_fini_array_callbacks() {
 } // namespace LIBC_NAMESPACE
 
 using LIBC_NAMESPACE::app;
-
-// TODO: Would be nice to use the aux entry structure from elf.h when available.
-struct AuxEntry {
-  LIBC_NAMESPACE::AuxEntryType type;
-  LIBC_NAMESPACE::AuxEntryType value;
-};
+using LIBC_NAMESPACE::AuxEntry;
 
 #if defined(LIBC_TARGET_ARCH_IS_X86_64) ||                                     \
     defined(LIBC_TARGET_ARCH_IS_AARCH64) ||                                    \
@@ -158,9 +153,9 @@ __attribute__((noinline)) static void do_start() {
   // denoted by an AT_NULL entry.
   PgrHdrTableType *program_hdr_table = nullptr;
   uintptr_t program_hdr_count;
-  for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
-       aux_entry->type != AT_NULL; ++aux_entry) {
-    switch (aux_entry->type) {
+  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<PgrHdrTableType *>(aux_entry->value);
       break;
diff --git a/libc/startup/linux/x86_64/start.cpp b/libc/startup/linux/x86_64/start.cpp
index 496105dfd0b43a..c98f58a4ac0aff 100644
--- a/libc/startup/linux/x86_64/start.cpp
+++ b/libc/startup/linux/x86_64/start.cpp
@@ -144,12 +144,7 @@ static void call_fini_array_callbacks() {
 } // namespace LIBC_NAMESPACE
 
 using LIBC_NAMESPACE::app;
-
-// TODO: Would be nice to use the aux entry structure from elf.h when available.
-struct AuxEntry {
-  uint64_t type;
-  uint64_t value;
-};
+using LIBC_NAMESPACE::AuxEntry;
 
 extern "C" void _start() {
   // This TU is compiled with -fno-omit-frame-pointer. Hence, the previous value
@@ -193,9 +188,9 @@ extern "C" void _start() {
   // denoted by an AT_NULL entry.
   Elf64_Phdr *program_hdr_table = nullptr;
   uintptr_t program_hdr_count = 0;
-  for (AuxEntry *aux_entry = reinterpret_cast<AuxEntry *>(env_end_marker + 1);
-       aux_entry->type != AT_NULL; ++aux_entry) {
-    switch (aux_entry->type) {
+  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);
       break;



More information about the libc-commits mailing list