xref: /aosp_15_r20/bionic/linker/linker_auxv.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "linker_auxv.h"
30 
31 #include <elf.h>
32 #include <stdio.h>
33 #include <sys/auxv.h>
34 #include <unistd.h>
35 
36 #include <async_safe/log.h>
37 
auxv_name(int at)38 static const char* auxv_name(int at) {
39   switch (at) {
40   case AT_NULL: return "AT_NULL";
41   case AT_IGNORE: return "AT_IGNORE";
42   case AT_EXECFD: return "AT_EXECFD";
43   case AT_PHDR: return "AT_PHDR";
44   case AT_PHENT: return "AT_PHENT";
45   case AT_PHNUM: return "AT_PHNUM";
46   case AT_PAGESZ: return "AT_PAGESZ";
47   case AT_BASE: return "AT_BASE";
48   case AT_FLAGS: return "AT_FLAGS";
49   case AT_ENTRY: return "AT_ENTRY";
50   case AT_NOTELF: return "AT_NOTELF";
51   case AT_UID: return "AT_UID";
52   case AT_EUID: return "AT_EUID";
53   case AT_GID: return "AT_GID";
54   case AT_EGID: return "AT_EGID";
55   case AT_PLATFORM: return "AT_PLATFORM";
56   case AT_HWCAP: return "AT_HWCAP";
57   case AT_CLKTCK: return "AT_CLKTCK";
58   case AT_SECURE: return "AT_SECURE";
59   case AT_BASE_PLATFORM: return "AT_BASE_PLATFORM";
60   case AT_RANDOM: return "AT_RANDOM";
61   case AT_HWCAP2: return "AT_HWCAP2";
62   case AT_RSEQ_FEATURE_SIZE: return "AT_RSEQ_FEATURE_SIZE";
63   case AT_RSEQ_ALIGN: return "AT_RSEQ_ALIGN";
64   case AT_HWCAP3: return "AT_HWCAP3";
65   case AT_HWCAP4: return "AT_HWCAP4";
66   case AT_EXECFN: return "AT_EXECFN";
67   case AT_SYSINFO_EHDR: return "AT_SYSINFO_EHDR";
68   case AT_MINSIGSTKSZ: return "AT_MINSIGSTKSZ";
69 #if defined(AT_VECTOR_SIZE_ARCH)
70   // AT_VECTOR_SIZE_ARCH isn't a value: it's the number of architecture-specific
71   // values that exist for the current architecture, so not relevant here.
72 #endif
73 #if defined(AT_SYSINFO)
74   case AT_SYSINFO: return "AT_SYSINFO";
75 #endif
76 #if defined(AT_L1I_CACHESIZE)
77   case AT_L1I_CACHESIZE: return "AT_L1I_CACHESIZE";
78 #endif
79 #if defined(AT_L1I_CACHEGEOMETRY)
80   case AT_L1I_CACHEGEOMETRY: return "AT_L1I_CACHEGEOMETRY";
81 #endif
82 #if defined(AT_L1D_CACHESIZE)
83   case AT_L1D_CACHESIZE: return "AT_L1D_CACHESIZE";
84 #endif
85 #if defined(AT_L1D_CACHEGEOMETRY)
86   case AT_L1D_CACHEGEOMETRY: return "AT_L1D_CACHEGEOMETRY";
87 #endif
88 #if defined(AT_L2_CACHESIZE)
89   case AT_L2_CACHESIZE: return "AT_L2_CACHESIZE";
90 #endif
91 #if defined(AT_L2_CACHEGEOMETRY)
92   case AT_L2_CACHEGEOMETRY: return "AT_L2_CACHEGEOMETRY";
93 #endif
94 #if defined(AT_L3_CACHESIZE)
95   case AT_L3_CACHESIZE: return "AT_L3_CACHESIZE";
96 #endif
97 #if defined(AT_L3_CACHEGEOMETRY)
98   case AT_L3_CACHEGEOMETRY: return "AT_L3_CACHEGEOMETRY";
99 #endif
100   }
101   static char name[32];
102   snprintf(name, sizeof(name), "AT_??? (%d)", at);
103   return name;
104 }
105 
ld_show_auxv(ElfW (auxv_t)* auxv)106 void ld_show_auxv(ElfW(auxv_t)* auxv) {
107   for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) {
108     const char* name = auxv_name(v->a_type);
109     long value = v->a_un.a_val;
110     switch (v->a_type) {
111     case AT_SYSINFO_EHDR:
112     case AT_PHDR:
113     case AT_BASE:
114     case AT_ENTRY:
115     case AT_RANDOM:
116       async_safe_format_fd(STDOUT_FILENO, "%-20s %#lx\n", name, value);
117       break;
118     case AT_FLAGS:
119     case AT_HWCAP:
120     case AT_HWCAP2:
121     case AT_HWCAP3:
122     case AT_HWCAP4:
123       async_safe_format_fd(STDOUT_FILENO, "%-20s %#lb\n", name, value);
124       break;
125     case AT_EXECFN:
126     case AT_PLATFORM:
127       async_safe_format_fd(STDOUT_FILENO, "%-20s \"%s\"\n", name, reinterpret_cast<char*>(value));
128       break;
129     default:
130       async_safe_format_fd(STDOUT_FILENO, "%-20s %ld\n", name, value);
131       break;
132     }
133   }
134 }
135