Skip to content

Conversation

SchrodingerZhu
Copy link
Contributor

@SchrodingerZhu SchrodingerZhu commented Oct 8, 2025

Core Refactoring: Remove dependencies on external getauxval across libc

Replaced direct getauxval() calls with internal auxv interface:

Modified files:

  • libc/src/__support/OSUtil/linux/vdso.cpp:

    • Replaced getauxval(AT_SYSINFO_EHDR) with auxv::get(AT_SYSINFO_EHDR)
    • Added proper error handling with optional return type
    • Remove obsolete errno protection logic
  • libc/src/unistd/linux/sysconf.cpp:

    • Replaced getauxval(AT_PAGESZ) with auxv::get(AT_PAGESZ)
    • Added error handling for page size retrieval

Purpose: This refactoring removes direct dependencies on the getauxval entrypoint from internal libc code, making the implementation more modular and reducing coupling.

Remaining Uses of libc.src.sys.auxv.getauxval

The following CMake files still reference libc.src.sys.auxv.getauxval:

Test Infrastructure:

  1. libc/test/IntegrationTest/CMakeLists.txt (line 3):

    • Sets arch_specific_deps for integration tests
  2. libc/test/integration/scudo/CMakeLists.txt (line 24):

    • Scudo integration test dependencies
  3. libc/cmake/modules/LLVMLibCTestRules.cmake (lines 839-845):

    • Temporary workaround for AArch64 hermetic tests
    • Comment indicates "dependency chain is broken" for getauxval propagation

Unit Tests:

  1. libc/test/src/sys/auxv/linux/CMakeLists.txt (line 11):
    • Unit test for the getauxval function itself

@llvmbot llvmbot added the libc label Oct 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Oct 8, 2025

@llvm/pr-subscribers-libc

Author: Schrodinger ZHU Yifan (SchrodingerZhu)

Changes

Core Refactoring: Auxiliary Vector (auxv) Implementation

Replaced direct getauxval() calls with internal auxv interface:

  1. New auxv support infrastructure:

    • Added libc.src.__support.OSUtil.linux.auxv - internal auxv interface
    • Provides auxv::get(AT_*) function returning cpp::optional<unsigned long>
  2. Modified files:

    • libc/src/__support/OSUtil/linux/vdso.cpp:

      • Replaced getauxval(AT_SYSINFO_EHDR) with auxv::get(AT_SYSINFO_EHDR)
      • Added proper error handling with optional return type
    • libc/src/unistd/linux/sysconf.cpp:

      • Replaced getauxval(AT_PAGESZ) with auxv::get(AT_PAGESZ)
      • Added error handling for page size retrieval
  3. Updated dependencies:

    • libc/src/__support/OSUtil/linux/CMakeLists.txt: Added auxv dependency
    • libc/src/unistd/linux/CMakeLists.txt: Replaced getauxval dependency with auxv

Purpose: This refactoring removes direct dependencies on the getauxval entrypoint from internal libc code, making the implementation more modular and reducing coupling.

Remaining Uses of libc.src.sys.auxv.getauxval

The following CMake files still reference libc.src.sys.auxv.getauxval:

Test Infrastructure:

  1. libc/test/IntegrationTest/CMakeLists.txt (line 3):

    • Sets arch_specific_deps for integration tests
  2. libc/test/integration/scudo/CMakeLists.txt (line 24):

    • Scudo integration test dependencies
  3. libc/cmake/modules/LLVMLibCTestRules.cmake (lines 839-845):

    • Temporary workaround for AArch64 hermetic tests
    • Comment indicates "dependency chain is broken" for getauxval propagation

Unit Tests:

  1. libc/test/src/sys/auxv/linux/CMakeLists.txt (line 11):
    • Unit test for the getauxval function itself

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

4 Files Affected:

  • (modified) libc/src/__support/OSUtil/linux/CMakeLists.txt (+2-2)
  • (modified) libc/src/__support/OSUtil/linux/vdso.cpp (+3-2)
  • (modified) libc/src/unistd/linux/CMakeLists.txt (+2-2)
  • (modified) libc/src/unistd/linux/sysconf.cpp (+7-4)
diff --git a/libc/src/__support/OSUtil/linux/CMakeLists.txt b/libc/src/__support/OSUtil/linux/CMakeLists.txt
index f6377ca9ff5a2..dd06854eb65f3 100644
--- a/libc/src/__support/OSUtil/linux/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/CMakeLists.txt
@@ -70,11 +70,11 @@ add_object_library(
     libc.src.__support.CPP.string_view
     libc.src.__support.threads.callonce
     libc.src.__support.threads.linux.futex_word_type
+    libc.src.__support.libc_errno
+    libc.src.__support.OSUtil.linux.auxv
     libc.hdr.types.struct_timeval
     libc.hdr.types.struct_timespec
     libc.hdr.types.clockid_t
     libc.hdr.types.time_t
     libc.hdr.link_macros
-    libc.src.errno.errno
-    libc.src.sys.auxv.getauxval
 )
diff --git a/libc/src/__support/OSUtil/linux/vdso.cpp b/libc/src/__support/OSUtil/linux/vdso.cpp
index e4e53c3c2a0f2..3a6369579ce5f 100644
--- a/libc/src/__support/OSUtil/linux/vdso.cpp
+++ b/libc/src/__support/OSUtil/linux/vdso.cpp
@@ -11,10 +11,10 @@
 #include "src/__support/CPP/array.h"
 #include "src/__support/CPP/optional.h"
 #include "src/__support/CPP/string_view.h"
+#include "src/__support/OSUtil/linux/auxv.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/threads/callonce.h"
 #include "src/__support/threads/linux/futex_word.h"
-#include "src/sys/auxv/getauxval.h"
 #include <linux/auxvec.h>
 
 // TODO: This is a temporary workaround to avoid including elf.h
@@ -192,7 +192,8 @@ void Symbol::initialize_vdso_global_cache() {
   // get the address of the VDSO, protect errno since getauxval may change
   // it
   int errno_backup = libc_errno;
-  uintptr_t vdso_ehdr_addr = getauxval(AT_SYSINFO_EHDR);
+  cpp::optional<unsigned long> auxv_res = auxv::get(AT_SYSINFO_EHDR);
+  uintptr_t vdso_ehdr_addr = auxv_res ? static_cast<uintptr_t>(*auxv_res) : 0;
   // Get the memory address of the vDSO ELF header.
   auto vdso_ehdr = reinterpret_cast<ElfW(Ehdr) *>(vdso_ehdr_addr);
   // leave the table unpopulated if we don't have vDSO
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index dff6ba2db8a38..4eb3c7d3d7fae 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -563,8 +563,8 @@ add_entrypoint_object(
   DEPENDS
     libc.include.unistd
     libc.include.sys_auxv
-    libc.src.errno.errno
-    libc.src.sys.auxv.getauxval
+    libc.src.__support.libc_errno
+    libc.src.__support.OSUtil.linux.auxv
 )
 
 add_entrypoint_object(
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index 03f224b150273..979e178cb45ee 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -11,17 +11,20 @@
 #include "src/__support/common.h"
 
 #include "hdr/unistd_macros.h"
+#include "src/__support/OSUtil/linux/auxv.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
-#include "src/sys/auxv/getauxval.h"
-#include <sys/auxv.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
   long ret = 0;
-  if (name == _SC_PAGESIZE)
-    return static_cast<long>(getauxval(AT_PAGESZ));
+  if (name == _SC_PAGESIZE) {
+    cpp::optional<unsigned long> page_size = auxv::get(AT_PAGESZ);
+    if (page_size)
+      return static_cast<long>(*page_size);
+    ret = -1;
+  }
 
   // TODO: Complete the rest of the sysconf options.
   if (ret < 0) {

@SchrodingerZhu SchrodingerZhu merged commit 23981c4 into llvm:main Oct 8, 2025
19 checks passed
clingfei pushed a commit to clingfei/llvm-project that referenced this pull request Oct 10, 2025
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.

3 participants