1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <endian.h>
5 
6 #include "macro.h"
7 #include "util.h"
8 
9 /* A cleaned up architecture definition. We don't want to get lost in
10  * processor features, models, generations or even ABIs. Hence we
11  * focus on general family, and distinguish word width and endianness. */
12 
13 typedef enum {
14         ARCHITECTURE_ALPHA,
15         ARCHITECTURE_ARC,
16         ARCHITECTURE_ARC_BE,
17         ARCHITECTURE_ARM,
18         ARCHITECTURE_ARM64,
19         ARCHITECTURE_ARM64_BE,
20         ARCHITECTURE_ARM_BE,
21         ARCHITECTURE_CRIS,
22         ARCHITECTURE_IA64,
23         ARCHITECTURE_LOONGARCH64,
24         ARCHITECTURE_M68K,
25         ARCHITECTURE_MIPS,
26         ARCHITECTURE_MIPS64,
27         ARCHITECTURE_MIPS64_LE,
28         ARCHITECTURE_MIPS_LE,
29         ARCHITECTURE_NIOS2,
30         ARCHITECTURE_PARISC,
31         ARCHITECTURE_PARISC64,
32         ARCHITECTURE_PPC,
33         ARCHITECTURE_PPC64,
34         ARCHITECTURE_PPC64_LE,
35         ARCHITECTURE_PPC_LE,
36         ARCHITECTURE_RISCV32,
37         ARCHITECTURE_RISCV64,
38         ARCHITECTURE_S390,
39         ARCHITECTURE_S390X,
40         ARCHITECTURE_SH,
41         ARCHITECTURE_SH64,
42         ARCHITECTURE_SPARC,
43         ARCHITECTURE_SPARC64,
44         ARCHITECTURE_TILEGX,
45         ARCHITECTURE_X86,
46         ARCHITECTURE_X86_64,
47         _ARCHITECTURE_MAX,
48         _ARCHITECTURE_INVALID = -EINVAL,
49 } Architecture;
50 
51 Architecture uname_architecture(void);
52 
53 /*
54  * LIB_ARCH_TUPLE should resolve to the local library path
55  * architecture tuple systemd is built for, according to the Debian
56  * tuple list:
57  *
58  * https://wiki.debian.org/Multiarch/Tuples
59  *
60  * This is used in library search paths that should understand
61  * Debian's paths on all distributions.
62  */
63 
64 #if defined(__x86_64__)
65 #  define native_architecture() ARCHITECTURE_X86_64
66 #  if defined(__ILP32__)
67 #    define LIB_ARCH_TUPLE "x86_64-linux-gnux32"
68 #  else
69 #    define LIB_ARCH_TUPLE "x86_64-linux-gnu"
70 #  endif
71 #  define ARCHITECTURE_SECONDARY ARCHITECTURE_X86
72 #elif defined(__i386__)
73 #  define native_architecture() ARCHITECTURE_X86
74 #  define LIB_ARCH_TUPLE "i386-linux-gnu"
75 #elif defined(__powerpc64__)
76 #  if __BYTE_ORDER == __BIG_ENDIAN
77 #    define native_architecture() ARCHITECTURE_PPC64
78 #    define LIB_ARCH_TUPLE "ppc64-linux-gnu"
79 #    define ARCHITECTURE_SECONDARY ARCHITECTURE_PPC
80 #  else
81 #    define native_architecture() ARCHITECTURE_PPC64_LE
82 #    define LIB_ARCH_TUPLE  "powerpc64le-linux-gnu"
83 #    define ARCHITECTURE_SECONDARY ARCHITECTURE_PPC_LE
84 #  endif
85 #elif defined(__powerpc__)
86 #  if __BYTE_ORDER == __BIG_ENDIAN
87 #    define native_architecture() ARCHITECTURE_PPC
88 #    if defined(__NO_FPRS__)
89 #      define LIB_ARCH_TUPLE "powerpc-linux-gnuspe"
90 #    else
91 #      define LIB_ARCH_TUPLE "powerpc-linux-gnu"
92 #    endif
93 #  else
94 #    define native_architecture() ARCHITECTURE_PPC_LE
95 #    error "Missing LIB_ARCH_TUPLE for PPCLE"
96 #  endif
97 #elif defined(__ia64__)
98 #  define native_architecture() ARCHITECTURE_IA64
99 #  define LIB_ARCH_TUPLE "ia64-linux-gnu"
100 #elif defined(__hppa64__)
101 #  define native_architecture() ARCHITECTURE_PARISC64
102 #  error "Missing LIB_ARCH_TUPLE for HPPA64"
103 #elif defined(__hppa__)
104 #  define native_architecture() ARCHITECTURE_PARISC
105 #  define LIB_ARCH_TUPLE "hppa‑linux‑gnu"
106 #elif defined(__s390x__)
107 #  define native_architecture() ARCHITECTURE_S390X
108 #  define LIB_ARCH_TUPLE "s390x-linux-gnu"
109 #  define ARCHITECTURE_SECONDARY ARCHITECTURE_S390
110 #elif defined(__s390__)
111 #  define native_architecture() ARCHITECTURE_S390
112 #  define LIB_ARCH_TUPLE "s390-linux-gnu"
113 #elif defined(__sparc__) && defined (__arch64__)
114 #  define native_architecture() ARCHITECTURE_SPARC64
115 #  define LIB_ARCH_TUPLE "sparc64-linux-gnu"
116 #elif defined(__sparc__)
117 #  define native_architecture() ARCHITECTURE_SPARC
118 #  define LIB_ARCH_TUPLE "sparc-linux-gnu"
119 #elif defined(__mips64) && defined(__LP64__)
120 #  if __BYTE_ORDER == __BIG_ENDIAN
121 #    define native_architecture() ARCHITECTURE_MIPS64
122 #    define LIB_ARCH_TUPLE "mips64-linux-gnuabi64"
123 #  else
124 #    define native_architecture() ARCHITECTURE_MIPS64_LE
125 #    define LIB_ARCH_TUPLE "mips64el-linux-gnuabi64"
126 #  endif
127 #elif defined(__mips64)
128 #  if __BYTE_ORDER == __BIG_ENDIAN
129 #    define native_architecture() ARCHITECTURE_MIPS64
130 #    define LIB_ARCH_TUPLE "mips64-linux-gnuabin32"
131 #  else
132 #    define native_architecture() ARCHITECTURE_MIPS64_LE
133 #    define LIB_ARCH_TUPLE "mips64el-linux-gnuabin32"
134 #  endif
135 #elif defined(__mips__)
136 #  if __BYTE_ORDER == __BIG_ENDIAN
137 #    define native_architecture() ARCHITECTURE_MIPS
138 #    define LIB_ARCH_TUPLE "mips-linux-gnu"
139 #  else
140 #    define native_architecture() ARCHITECTURE_MIPS_LE
141 #    define LIB_ARCH_TUPLE "mipsel-linux-gnu"
142 #  endif
143 #elif defined(__alpha__)
144 #  define native_architecture() ARCHITECTURE_ALPHA
145 #  define LIB_ARCH_TUPLE "alpha-linux-gnu"
146 #elif defined(__aarch64__)
147 #  if __BYTE_ORDER == __BIG_ENDIAN
148 #    define native_architecture() ARCHITECTURE_ARM64_BE
149 #    define LIB_ARCH_TUPLE "aarch64_be-linux-gnu"
150 #  else
151 #    define native_architecture() ARCHITECTURE_ARM64
152 #    define LIB_ARCH_TUPLE "aarch64-linux-gnu"
153 #    define ARCHITECTURE_SECONDARY ARCHITECTURE_ARM
154 #  endif
155 #elif defined(__arm__)
156 #  if __BYTE_ORDER == __BIG_ENDIAN
157 #    define native_architecture() ARCHITECTURE_ARM_BE
158 #    if defined(__ARM_EABI__)
159 #      if defined(__ARM_PCS_VFP)
160 #        define LIB_ARCH_TUPLE "armeb-linux-gnueabihf"
161 #      else
162 #        define LIB_ARCH_TUPLE "armeb-linux-gnueabi"
163 #      endif
164 #    else
165 #      define LIB_ARCH_TUPLE "armeb-linux-gnu"
166 #    endif
167 #  else
168 #    define native_architecture() ARCHITECTURE_ARM
169 #    if defined(__ARM_EABI__)
170 #      if defined(__ARM_PCS_VFP)
171 #        define LIB_ARCH_TUPLE "arm-linux-gnueabihf"
172 #      else
173 #        define LIB_ARCH_TUPLE "arm-linux-gnueabi"
174 #      endif
175 #    else
176 #      define LIB_ARCH_TUPLE "arm-linux-gnu"
177 #    endif
178 #  endif
179 #elif defined(__sh64__)
180 #  define native_architecture() ARCHITECTURE_SH64
181 #  error "Missing LIB_ARCH_TUPLE for SH64"
182 #elif defined(__sh__)
183 #  define native_architecture() ARCHITECTURE_SH
184 #  if defined(__SH1__)
185 #    define LIB_ARCH_TUPLE "sh1-linux-gnu"
186 #  elif defined(__SH2__)
187 #    define LIB_ARCH_TUPLE "sh2-linux-gnu"
188 #  elif defined(__SH2A__)
189 #    define LIB_ARCH_TUPLE "sh2a-linux-gnu"
190 #  elif defined(__SH2E__)
191 #    define LIB_ARCH_TUPLE "sh2e-linux-gnu"
192 #  elif defined(__SH3__)
193 #    define LIB_ARCH_TUPLE "sh3-linux-gnu"
194 #  elif defined(__SH3E__)
195 #    define LIB_ARCH_TUPLE "sh3e-linux-gnu"
196 #  elif defined(__SH4__) && !defined(__SH4A__)
197 #    define LIB_ARCH_TUPLE "sh4-linux-gnu"
198 #  elif defined(__SH4A__)
199 #    define LIB_ARCH_TUPLE "sh4a-linux-gnu"
200 #  endif
201 #elif defined(__loongarch64)
202 #  define native_architecture() ARCHITECTURE_LOONGARCH64
203 #  if defined(__loongarch_double_float)
204 #    define LIB_ARCH_TUPLE "loongarch64-linux-gnuf64"
205 #  elif defined(__loongarch_single_float)
206 #    define LIB_ARCH_TUPLE "loongarch64-linux-gnuf32"
207 #  elif defined(__loongarch_soft_float)
208 #    define LIB_ARCH_TUPLE "loongarch64-linux-gnusf"
209 #  else
210 #    error "Unrecognized loongarch architecture variant"
211 #  endif
212 #elif defined(__m68k__)
213 #  define native_architecture() ARCHITECTURE_M68K
214 #  define LIB_ARCH_TUPLE "m68k-linux-gnu"
215 #elif defined(__tilegx__)
216 #  define native_architecture() ARCHITECTURE_TILEGX
217 #  define LIB_ARCH_TUPLE "tilegx-linux-gnu"
218 #elif defined(__cris__)
219 #  define native_architecture() ARCHITECTURE_CRIS
220 #  error "Missing LIB_ARCH_TUPLE for CRIS"
221 #elif defined(__nios2__)
222 #  define native_architecture() ARCHITECTURE_NIOS2
223 #  define LIB_ARCH_TUPLE "nios2-linux-gnu"
224 #elif defined(__riscv)
225 #  if __SIZEOF_POINTER__ == 4
226 #    define native_architecture() ARCHITECTURE_RISCV32
227 #    define LIB_ARCH_TUPLE "riscv32-linux-gnu"
228 #  elif __SIZEOF_POINTER__ == 8
229 #    define native_architecture() ARCHITECTURE_RISCV64
230 #    define LIB_ARCH_TUPLE "riscv64-linux-gnu"
231 #  else
232 #    error "Unrecognized riscv architecture variant"
233 #  endif
234 #elif defined(__arc__)
235 #  if __BYTE_ORDER == __BIG_ENDIAN
236 #    define native_architecture() ARCHITECTURE_ARC_BE
237 #    define LIB_ARCH_TUPLE "arceb-linux"
238 #  else
239 #    define native_architecture() ARCHITECTURE_ARC
240 #    define LIB_ARCH_TUPLE "arc-linux"
241 #  endif
242 #else
243 #  error "Please register your architecture here!"
244 #endif
245 
246 const char *architecture_to_string(Architecture a) _const_;
247 Architecture architecture_from_string(const char *s) _pure_;
248