Merge "linker: add LD_SHOW_AUXV support." into main
diff --git a/docs/status.md b/docs/status.md
index 5514935..de2fa10 100644
--- a/docs/status.md
+++ b/docs/status.md
@@ -65,6 +65,10 @@
   * New system call wrappers: `__riscv_flush_icache` (`<sys/cachectl.h>`),
     `__riscv_hwprobe` (`<sys/hwprobe.h>`).
 
+New libc behavior in V (API level 35):
+  * Added `LD_SHOW_AUXV` to the dynamic linker to dump the ELF auxiliary
+    vector if the environment variable is set.
+
 New libc functions in U (API level 34):
   * `close_range` and `copy_file_range` (Linux-specific GNU extensions).
   * `memset_explicit` in <string.h> (C23 addition).
diff --git a/linker/Android.bp b/linker/Android.bp
index 020bd7d..0ccd16d 100644
--- a/linker/Android.bp
+++ b/linker/Android.bp
@@ -169,6 +169,7 @@
     srcs: [
         "dlfcn.cpp",
         "linker.cpp",
+        "linker_auxv.cpp",
         "linker_block_allocator.cpp",
         "linker_dlwarning.cpp",
         "linker_cfi.cpp",
diff --git a/linker/NOTICE b/linker/NOTICE
index d61a193..7fd1877 100644
--- a/linker/NOTICE
+++ b/linker/NOTICE
@@ -362,3 +362,31 @@
 
 -------------------------------------------------------------------
 
+Copyright (C) 2023 The Android Open Source Project
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+ * Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in
+   the documentation and/or other materials provided with the
+   distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGE.
+
+-------------------------------------------------------------------
+
diff --git a/linker/linker_auxv.cpp b/linker/linker_auxv.cpp
new file mode 100644
index 0000000..d8e4a3e
--- /dev/null
+++ b/linker/linker_auxv.cpp
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "linker_auxv.h"
+
+#include <elf.h>
+#include <stdio.h>
+#include <sys/auxv.h>
+#include <unistd.h>
+
+#include <async_safe/log.h>
+
+static const char* auxv_name(int at) {
+  switch (at) {
+  case AT_NULL: return "AT_NULL";
+  case AT_IGNORE: return "AT_IGNORE";
+  case AT_EXECFD: return "AT_EXECFD";
+  case AT_PHDR: return "AT_PHDR";
+  case AT_PHENT: return "AT_PHENT";
+  case AT_PHNUM: return "AT_PHNUM";
+  case AT_PAGESZ: return "AT_PAGESZ";
+  case AT_BASE: return "AT_BASE";
+  case AT_FLAGS: return "AT_FLAGS";
+  case AT_ENTRY: return "AT_ENTRY";
+  case AT_NOTELF: return "AT_NOTELF";
+  case AT_UID: return "AT_UID";
+  case AT_EUID: return "AT_EUID";
+  case AT_GID: return "AT_GID";
+  case AT_EGID: return "AT_EGID";
+  case AT_PLATFORM: return "AT_PLATFORM";
+  case AT_HWCAP: return "AT_HWCAP";
+  case AT_CLKTCK: return "AT_CLKTCK";
+  case AT_SECURE: return "AT_SECURE";
+  case AT_BASE_PLATFORM: return "AT_BASE_PLATFORM";
+  case AT_RANDOM: return "AT_RANDOM";
+  case AT_HWCAP2: return "AT_HWCAP2";
+  case AT_RSEQ_FEATURE_SIZE: return "AT_RSEQ_FEATURE_SIZE";
+  case AT_RSEQ_ALIGN: return "AT_RSEQ_ALIGN";
+  case AT_EXECFN: return "AT_EXECFN";
+  case AT_SYSINFO_EHDR: return "AT_SYSINFO_EHDR";
+#if defined(AT_MINSIGSTKSZ)
+  case AT_MINSIGSTKSZ: return "AT_MINSIGSTKSZ";
+#endif
+#if defined(AT_SYSINFO)
+  case AT_SYSINFO: return "AT_SYSINFO";
+#endif
+#if defined(AT_L1I_CACHESIZE)
+  case AT_L1I_CACHESIZE: return "AT_L1I_CACHESIZE";
+#endif
+#if defined(AT_L1I_CACHEGEOMETRY)
+  case AT_L1I_CACHEGEOMETRY: return "AT_L1I_CACHEGEOMETRY";
+#endif
+#if defined(AT_L1D_CACHESIZE)
+  case AT_L1D_CACHESIZE: return "AT_L1D_CACHESIZE";
+#endif
+#if defined(AT_L1D_CACHEGEOMETRY)
+  case AT_L1D_CACHEGEOMETRY: return "AT_L1D_CACHEGEOMETRY";
+#endif
+#if defined(AT_L2_CACHESIZE)
+  case AT_L2_CACHESIZE: return "AT_L2_CACHESIZE";
+#endif
+#if defined(AT_L2_CACHEGEOMETRY)
+  case AT_L2_CACHEGEOMETRY: return "AT_L2_CACHEGEOMETRY";
+#endif
+  }
+  static char name[32];
+  snprintf(name, sizeof(name), "AT_??? (%d)", at);
+  return name;
+}
+
+void ld_show_auxv(ElfW(auxv_t)* auxv) {
+  for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) {
+    const char* name = auxv_name(v->a_type);
+    long value = v->a_un.a_val;
+    switch (v->a_type) {
+    case AT_SYSINFO_EHDR:
+    case AT_PHDR:
+    case AT_BASE:
+    case AT_ENTRY:
+    case AT_RANDOM:
+      async_safe_format_fd(STDOUT_FILENO, "%-20s %#lx\n", name, value);
+      break;
+    case AT_FLAGS:
+    case AT_HWCAP:
+    case AT_HWCAP2:
+      async_safe_format_fd(STDOUT_FILENO, "%-20s %#lb\n", name, value);
+      break;
+    case AT_EXECFN:
+    case AT_PLATFORM:
+      async_safe_format_fd(STDOUT_FILENO, "%-20s \"%s\"\n", name, reinterpret_cast<char*>(value));
+      break;
+    default:
+      async_safe_format_fd(STDOUT_FILENO, "%-20s %ld\n", name, value);
+      break;
+    }
+  }
+}
diff --git a/linker/linker_auxv.h b/linker/linker_auxv.h
new file mode 100644
index 0000000..c093283
--- /dev/null
+++ b/linker/linker_auxv.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <elf.h>
+#include <link.h>
+
+void ld_show_auxv(ElfW(auxv_t)* auxv);
diff --git a/linker/linker_main.cpp b/linker/linker_main.cpp
index 8a20670..5a33a63 100644
--- a/linker/linker_main.cpp
+++ b/linker/linker_main.cpp
@@ -32,6 +32,7 @@
 #include <sys/auxv.h>
 
 #include "linker.h"
+#include "linker_auxv.h"
 #include "linker_cfi.h"
 #include "linker_debug.h"
 #include "linker_debuggerd.h"
@@ -324,12 +325,14 @@
 
   g_linker_logger.ResetState();
 
-  // Get a few environment variables.
+  // Enable debugging logs?
   const char* LD_DEBUG = getenv("LD_DEBUG");
   if (LD_DEBUG != nullptr) {
     g_ld_debug_verbosity = atoi(LD_DEBUG);
   }
 
+  if (getenv("LD_SHOW_AUXV") != nullptr) ld_show_auxv(args.auxv);
+
 #if defined(__LP64__)
   INFO("[ Android dynamic linker (64-bit) ]");
 #else