1 //===-- ArchSpec.cpp --------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Utility/ArchSpec.h"
11 
12 #include "lldb/Utility/NameMatches.h"
13 #include "lldb/Utility/Stream.h" // for Stream
14 #include "lldb/Utility/StringList.h"
15 #include "lldb/lldb-defines.h" // for LLDB_INVALID_C...
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Twine.h" // for Twine
18 #include "llvm/BinaryFormat/COFF.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/BinaryFormat/MachO.h" // for CPUType::CPU_T...
21 #include "llvm/Support/Compiler.h"   // for LLVM_FALLTHROUGH
22 #include "llvm/Support/Host.h"
23 
24 using namespace lldb;
25 using namespace lldb_private;
26 
27 static bool cores_match(const ArchSpec::Core core1, const ArchSpec::Core core2,
28                         bool try_inverse, bool enforce_exact_match);
29 
30 namespace lldb_private {
31 
32 struct CoreDefinition {
33   ByteOrder default_byte_order;
34   uint32_t addr_byte_size;
35   uint32_t min_opcode_byte_size;
36   uint32_t max_opcode_byte_size;
37   llvm::Triple::ArchType machine;
38   ArchSpec::Core core;
39   const char *const name;
40 };
41 
42 } // namespace lldb_private
43 
44 // This core information can be looked using the ArchSpec::Core as the index
45 static const CoreDefinition g_core_definitions[] = {
46     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_generic,
47      "arm"},
48     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv4,
49      "armv4"},
50     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv4t,
51      "armv4t"},
52     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5,
53      "armv5"},
54     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5e,
55      "armv5e"},
56     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv5t,
57      "armv5t"},
58     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv6,
59      "armv6"},
60     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv6m,
61      "armv6m"},
62     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7,
63      "armv7"},
64     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7f,
65      "armv7f"},
66     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7s,
67      "armv7s"},
68     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7k,
69      "armv7k"},
70     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7m,
71      "armv7m"},
72     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_armv7em,
73      "armv7em"},
74     {eByteOrderLittle, 4, 2, 4, llvm::Triple::arm, ArchSpec::eCore_arm_xscale,
75      "xscale"},
76     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumb,
77      "thumb"},
78     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv4t,
79      "thumbv4t"},
80     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv5,
81      "thumbv5"},
82     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv5e,
83      "thumbv5e"},
84     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv6,
85      "thumbv6"},
86     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv6m,
87      "thumbv6m"},
88     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7,
89      "thumbv7"},
90     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7f,
91      "thumbv7f"},
92     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7s,
93      "thumbv7s"},
94     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7k,
95      "thumbv7k"},
96     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7m,
97      "thumbv7m"},
98     {eByteOrderLittle, 4, 2, 4, llvm::Triple::thumb, ArchSpec::eCore_thumbv7em,
99      "thumbv7em"},
100     {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
101      ArchSpec::eCore_arm_arm64, "arm64"},
102     {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
103      ArchSpec::eCore_arm_armv8, "armv8"},
104     {eByteOrderLittle, 8, 4, 4, llvm::Triple::aarch64,
105      ArchSpec::eCore_arm_aarch64, "aarch64"},
106 
107     // mips32, mips32r2, mips32r3, mips32r5, mips32r6
108     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32,
109      "mips"},
110     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r2,
111      "mipsr2"},
112     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r3,
113      "mipsr3"},
114     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r5,
115      "mipsr5"},
116     {eByteOrderBig, 4, 2, 4, llvm::Triple::mips, ArchSpec::eCore_mips32r6,
117      "mipsr6"},
118     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel, ArchSpec::eCore_mips32el,
119      "mipsel"},
120     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
121      ArchSpec::eCore_mips32r2el, "mipsr2el"},
122     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
123      ArchSpec::eCore_mips32r3el, "mipsr3el"},
124     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
125      ArchSpec::eCore_mips32r5el, "mipsr5el"},
126     {eByteOrderLittle, 4, 2, 4, llvm::Triple::mipsel,
127      ArchSpec::eCore_mips32r6el, "mipsr6el"},
128 
129     // mips64, mips64r2, mips64r3, mips64r5, mips64r6
130     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64,
131      "mips64"},
132     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r2,
133      "mips64r2"},
134     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r3,
135      "mips64r3"},
136     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r5,
137      "mips64r5"},
138     {eByteOrderBig, 8, 2, 4, llvm::Triple::mips64, ArchSpec::eCore_mips64r6,
139      "mips64r6"},
140     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
141      ArchSpec::eCore_mips64el, "mips64el"},
142     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
143      ArchSpec::eCore_mips64r2el, "mips64r2el"},
144     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
145      ArchSpec::eCore_mips64r3el, "mips64r3el"},
146     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
147      ArchSpec::eCore_mips64r5el, "mips64r5el"},
148     {eByteOrderLittle, 8, 2, 4, llvm::Triple::mips64el,
149      ArchSpec::eCore_mips64r6el, "mips64r6el"},
150 
151     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_generic,
152      "powerpc"},
153     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc601,
154      "ppc601"},
155     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc602,
156      "ppc602"},
157     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603,
158      "ppc603"},
159     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603e,
160      "ppc603e"},
161     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc603ev,
162      "ppc603ev"},
163     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc604,
164      "ppc604"},
165     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc604e,
166      "ppc604e"},
167     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc620,
168      "ppc620"},
169     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc750,
170      "ppc750"},
171     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc7400,
172      "ppc7400"},
173     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc7450,
174      "ppc7450"},
175     {eByteOrderBig, 4, 4, 4, llvm::Triple::ppc, ArchSpec::eCore_ppc_ppc970,
176      "ppc970"},
177 
178     {eByteOrderLittle, 8, 4, 4, llvm::Triple::ppc64le,
179      ArchSpec::eCore_ppc64le_generic, "powerpc64le"},
180     {eByteOrderBig, 8, 4, 4, llvm::Triple::ppc64, ArchSpec::eCore_ppc64_generic,
181      "powerpc64"},
182     {eByteOrderBig, 8, 4, 4, llvm::Triple::ppc64,
183      ArchSpec::eCore_ppc64_ppc970_64, "ppc970-64"},
184 
185     {eByteOrderBig, 8, 2, 6, llvm::Triple::systemz,
186      ArchSpec::eCore_s390x_generic, "s390x"},
187 
188     {eByteOrderLittle, 4, 4, 4, llvm::Triple::sparc,
189      ArchSpec::eCore_sparc_generic, "sparc"},
190     {eByteOrderLittle, 8, 4, 4, llvm::Triple::sparcv9,
191      ArchSpec::eCore_sparc9_generic, "sparcv9"},
192 
193     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i386,
194      "i386"},
195     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i486,
196      "i486"},
197     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86,
198      ArchSpec::eCore_x86_32_i486sx, "i486sx"},
199     {eByteOrderLittle, 4, 1, 15, llvm::Triple::x86, ArchSpec::eCore_x86_32_i686,
200      "i686"},
201 
202     {eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64,
203      ArchSpec::eCore_x86_64_x86_64, "x86_64"},
204     {eByteOrderLittle, 8, 1, 15, llvm::Triple::x86_64,
205      ArchSpec::eCore_x86_64_x86_64h, "x86_64h"},
206     {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
207      ArchSpec::eCore_hexagon_generic, "hexagon"},
208     {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
209      ArchSpec::eCore_hexagon_hexagonv4, "hexagonv4"},
210     {eByteOrderLittle, 4, 4, 4, llvm::Triple::hexagon,
211      ArchSpec::eCore_hexagon_hexagonv5, "hexagonv5"},
212 
213     {eByteOrderLittle, 4, 4, 4, llvm::Triple::UnknownArch,
214      ArchSpec::eCore_uknownMach32, "unknown-mach-32"},
215     {eByteOrderLittle, 8, 4, 4, llvm::Triple::UnknownArch,
216      ArchSpec::eCore_uknownMach64, "unknown-mach-64"},
217 
218     {eByteOrderBig, 4, 1, 1, llvm::Triple::kalimba, ArchSpec::eCore_kalimba3,
219      "kalimba3"},
220     {eByteOrderLittle, 4, 1, 1, llvm::Triple::kalimba, ArchSpec::eCore_kalimba4,
221      "kalimba4"},
222     {eByteOrderLittle, 4, 1, 1, llvm::Triple::kalimba, ArchSpec::eCore_kalimba5,
223      "kalimba5"}};
224 
225 // Ensure that we have an entry in the g_core_definitions for each core. If you
226 // comment out an entry above,
227 // you will need to comment out the corresponding ArchSpec::Core enumeration.
228 static_assert(sizeof(g_core_definitions) / sizeof(CoreDefinition) ==
229                   ArchSpec::kNumCores,
230               "make sure we have one core definition for each core");
231 
232 struct ArchDefinitionEntry {
233   ArchSpec::Core core;
234   uint32_t cpu;
235   uint32_t sub;
236   uint32_t cpu_mask;
237   uint32_t sub_mask;
238 };
239 
240 struct ArchDefinition {
241   ArchitectureType type;
242   size_t num_entries;
243   const ArchDefinitionEntry *entries;
244   const char *name;
245 };
246 
247 size_t ArchSpec::AutoComplete(llvm::StringRef name, StringList &matches) {
248   if (!name.empty()) {
249     for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) {
250       if (NameMatches(g_core_definitions[i].name, NameMatch::StartsWith, name))
251         matches.AppendString(g_core_definitions[i].name);
252     }
253   } else {
254     for (uint32_t i = 0; i < llvm::array_lengthof(g_core_definitions); ++i)
255       matches.AppendString(g_core_definitions[i].name);
256   }
257   return matches.GetSize();
258 }
259 
260 #define CPU_ANY (UINT32_MAX)
261 
262 //===----------------------------------------------------------------------===//
263 // A table that gets searched linearly for matches. This table is used to
264 // convert cpu type and subtypes to architecture names, and to convert
265 // architecture names to cpu types and subtypes. The ordering is important and
266 // allows the precedence to be set when the table is built.
267 #define SUBTYPE_MASK 0x00FFFFFFu
268 
269 static const ArchDefinitionEntry g_macho_arch_entries[] = {
270     {ArchSpec::eCore_arm_generic, llvm::MachO::CPU_TYPE_ARM, CPU_ANY,
271      UINT32_MAX, UINT32_MAX},
272     {ArchSpec::eCore_arm_generic, llvm::MachO::CPU_TYPE_ARM, 0, UINT32_MAX,
273      SUBTYPE_MASK},
274     {ArchSpec::eCore_arm_armv4, llvm::MachO::CPU_TYPE_ARM, 5, UINT32_MAX,
275      SUBTYPE_MASK},
276     {ArchSpec::eCore_arm_armv4t, llvm::MachO::CPU_TYPE_ARM, 5, UINT32_MAX,
277      SUBTYPE_MASK},
278     {ArchSpec::eCore_arm_armv6, llvm::MachO::CPU_TYPE_ARM, 6, UINT32_MAX,
279      SUBTYPE_MASK},
280     {ArchSpec::eCore_arm_armv6m, llvm::MachO::CPU_TYPE_ARM, 14, UINT32_MAX,
281      SUBTYPE_MASK},
282     {ArchSpec::eCore_arm_armv5, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
283      SUBTYPE_MASK},
284     {ArchSpec::eCore_arm_armv5e, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
285      SUBTYPE_MASK},
286     {ArchSpec::eCore_arm_armv5t, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
287      SUBTYPE_MASK},
288     {ArchSpec::eCore_arm_xscale, llvm::MachO::CPU_TYPE_ARM, 8, UINT32_MAX,
289      SUBTYPE_MASK},
290     {ArchSpec::eCore_arm_armv7, llvm::MachO::CPU_TYPE_ARM, 9, UINT32_MAX,
291      SUBTYPE_MASK},
292     {ArchSpec::eCore_arm_armv7f, llvm::MachO::CPU_TYPE_ARM, 10, UINT32_MAX,
293      SUBTYPE_MASK},
294     {ArchSpec::eCore_arm_armv7s, llvm::MachO::CPU_TYPE_ARM, 11, UINT32_MAX,
295      SUBTYPE_MASK},
296     {ArchSpec::eCore_arm_armv7k, llvm::MachO::CPU_TYPE_ARM, 12, UINT32_MAX,
297      SUBTYPE_MASK},
298     {ArchSpec::eCore_arm_armv7m, llvm::MachO::CPU_TYPE_ARM, 15, UINT32_MAX,
299      SUBTYPE_MASK},
300     {ArchSpec::eCore_arm_armv7em, llvm::MachO::CPU_TYPE_ARM, 16, UINT32_MAX,
301      SUBTYPE_MASK},
302     {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, 1, UINT32_MAX,
303      SUBTYPE_MASK},
304     {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, 0, UINT32_MAX,
305      SUBTYPE_MASK},
306     {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, 13, UINT32_MAX,
307      SUBTYPE_MASK},
308     {ArchSpec::eCore_arm_arm64, llvm::MachO::CPU_TYPE_ARM64, CPU_ANY,
309      UINT32_MAX, SUBTYPE_MASK},
310     {ArchSpec::eCore_thumb, llvm::MachO::CPU_TYPE_ARM, 0, UINT32_MAX,
311      SUBTYPE_MASK},
312     {ArchSpec::eCore_thumbv4t, llvm::MachO::CPU_TYPE_ARM, 5, UINT32_MAX,
313      SUBTYPE_MASK},
314     {ArchSpec::eCore_thumbv5, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
315      SUBTYPE_MASK},
316     {ArchSpec::eCore_thumbv5e, llvm::MachO::CPU_TYPE_ARM, 7, UINT32_MAX,
317      SUBTYPE_MASK},
318     {ArchSpec::eCore_thumbv6, llvm::MachO::CPU_TYPE_ARM, 6, UINT32_MAX,
319      SUBTYPE_MASK},
320     {ArchSpec::eCore_thumbv6m, llvm::MachO::CPU_TYPE_ARM, 14, UINT32_MAX,
321      SUBTYPE_MASK},
322     {ArchSpec::eCore_thumbv7, llvm::MachO::CPU_TYPE_ARM, 9, UINT32_MAX,
323      SUBTYPE_MASK},
324     {ArchSpec::eCore_thumbv7f, llvm::MachO::CPU_TYPE_ARM, 10, UINT32_MAX,
325      SUBTYPE_MASK},
326     {ArchSpec::eCore_thumbv7s, llvm::MachO::CPU_TYPE_ARM, 11, UINT32_MAX,
327      SUBTYPE_MASK},
328     {ArchSpec::eCore_thumbv7k, llvm::MachO::CPU_TYPE_ARM, 12, UINT32_MAX,
329      SUBTYPE_MASK},
330     {ArchSpec::eCore_thumbv7m, llvm::MachO::CPU_TYPE_ARM, 15, UINT32_MAX,
331      SUBTYPE_MASK},
332     {ArchSpec::eCore_thumbv7em, llvm::MachO::CPU_TYPE_ARM, 16, UINT32_MAX,
333      SUBTYPE_MASK},
334     {ArchSpec::eCore_ppc_generic, llvm::MachO::CPU_TYPE_POWERPC, CPU_ANY,
335      UINT32_MAX, UINT32_MAX},
336     {ArchSpec::eCore_ppc_generic, llvm::MachO::CPU_TYPE_POWERPC, 0, UINT32_MAX,
337      SUBTYPE_MASK},
338     {ArchSpec::eCore_ppc_ppc601, llvm::MachO::CPU_TYPE_POWERPC, 1, UINT32_MAX,
339      SUBTYPE_MASK},
340     {ArchSpec::eCore_ppc_ppc602, llvm::MachO::CPU_TYPE_POWERPC, 2, UINT32_MAX,
341      SUBTYPE_MASK},
342     {ArchSpec::eCore_ppc_ppc603, llvm::MachO::CPU_TYPE_POWERPC, 3, UINT32_MAX,
343      SUBTYPE_MASK},
344     {ArchSpec::eCore_ppc_ppc603e, llvm::MachO::CPU_TYPE_POWERPC, 4, UINT32_MAX,
345      SUBTYPE_MASK},
346     {ArchSpec::eCore_ppc_ppc603ev, llvm::MachO::CPU_TYPE_POWERPC, 5, UINT32_MAX,
347      SUBTYPE_MASK},
348     {ArchSpec::eCore_ppc_ppc604, llvm::MachO::CPU_TYPE_POWERPC, 6, UINT32_MAX,
349      SUBTYPE_MASK},
350     {ArchSpec::eCore_ppc_ppc604e, llvm::MachO::CPU_TYPE_POWERPC, 7, UINT32_MAX,
351      SUBTYPE_MASK},
352     {ArchSpec::eCore_ppc_ppc620, llvm::MachO::CPU_TYPE_POWERPC, 8, UINT32_MAX,
353      SUBTYPE_MASK},
354     {ArchSpec::eCore_ppc_ppc750, llvm::MachO::CPU_TYPE_POWERPC, 9, UINT32_MAX,
355      SUBTYPE_MASK},
356     {ArchSpec::eCore_ppc_ppc7400, llvm::MachO::CPU_TYPE_POWERPC, 10, UINT32_MAX,
357      SUBTYPE_MASK},
358     {ArchSpec::eCore_ppc_ppc7450, llvm::MachO::CPU_TYPE_POWERPC, 11, UINT32_MAX,
359      SUBTYPE_MASK},
360     {ArchSpec::eCore_ppc_ppc970, llvm::MachO::CPU_TYPE_POWERPC, 100, UINT32_MAX,
361      SUBTYPE_MASK},
362     {ArchSpec::eCore_ppc64_generic, llvm::MachO::CPU_TYPE_POWERPC64, 0,
363      UINT32_MAX, SUBTYPE_MASK},
364     {ArchSpec::eCore_ppc64le_generic, llvm::MachO::CPU_TYPE_POWERPC64, CPU_ANY,
365      UINT32_MAX, SUBTYPE_MASK},
366     {ArchSpec::eCore_ppc64_ppc970_64, llvm::MachO::CPU_TYPE_POWERPC64, 100,
367      UINT32_MAX, SUBTYPE_MASK},
368     {ArchSpec::eCore_x86_32_i386, llvm::MachO::CPU_TYPE_I386, 3, UINT32_MAX,
369      SUBTYPE_MASK},
370     {ArchSpec::eCore_x86_32_i486, llvm::MachO::CPU_TYPE_I386, 4, UINT32_MAX,
371      SUBTYPE_MASK},
372     {ArchSpec::eCore_x86_32_i486sx, llvm::MachO::CPU_TYPE_I386, 0x84,
373      UINT32_MAX, SUBTYPE_MASK},
374     {ArchSpec::eCore_x86_32_i386, llvm::MachO::CPU_TYPE_I386, CPU_ANY,
375      UINT32_MAX, UINT32_MAX},
376     {ArchSpec::eCore_x86_64_x86_64, llvm::MachO::CPU_TYPE_X86_64, 3, UINT32_MAX,
377      SUBTYPE_MASK},
378     {ArchSpec::eCore_x86_64_x86_64, llvm::MachO::CPU_TYPE_X86_64, 4, UINT32_MAX,
379      SUBTYPE_MASK},
380     {ArchSpec::eCore_x86_64_x86_64h, llvm::MachO::CPU_TYPE_X86_64, 8,
381      UINT32_MAX, SUBTYPE_MASK},
382     {ArchSpec::eCore_x86_64_x86_64, llvm::MachO::CPU_TYPE_X86_64, CPU_ANY,
383      UINT32_MAX, UINT32_MAX},
384     // Catch any unknown mach architectures so we can always use the object and
385     // symbol mach-o files
386     {ArchSpec::eCore_uknownMach32, 0, 0, 0xFF000000u, 0x00000000u},
387     {ArchSpec::eCore_uknownMach64, llvm::MachO::CPU_ARCH_ABI64, 0, 0xFF000000u,
388      0x00000000u}};
389 
390 static const ArchDefinition g_macho_arch_def = {
391     eArchTypeMachO, llvm::array_lengthof(g_macho_arch_entries),
392     g_macho_arch_entries, "mach-o"};
393 
394 //===----------------------------------------------------------------------===//
395 // A table that gets searched linearly for matches. This table is used to
396 // convert cpu type and subtypes to architecture names, and to convert
397 // architecture names to cpu types and subtypes. The ordering is important and
398 // allows the precedence to be set when the table is built.
399 static const ArchDefinitionEntry g_elf_arch_entries[] = {
400     {ArchSpec::eCore_sparc_generic, llvm::ELF::EM_SPARC, LLDB_INVALID_CPUTYPE,
401      0xFFFFFFFFu, 0xFFFFFFFFu}, // Sparc
402     {ArchSpec::eCore_x86_32_i386, llvm::ELF::EM_386, LLDB_INVALID_CPUTYPE,
403      0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel 80386
404     {ArchSpec::eCore_x86_32_i486, llvm::ELF::EM_IAMCU, LLDB_INVALID_CPUTYPE,
405      0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel MCU // FIXME: is this correct?
406     {ArchSpec::eCore_ppc_generic, llvm::ELF::EM_PPC, LLDB_INVALID_CPUTYPE,
407      0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC
408     {ArchSpec::eCore_ppc64le_generic, llvm::ELF::EM_PPC64, LLDB_INVALID_CPUTYPE,
409      0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC64le
410     {ArchSpec::eCore_ppc64_generic, llvm::ELF::EM_PPC64, LLDB_INVALID_CPUTYPE,
411      0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC64
412     {ArchSpec::eCore_arm_generic, llvm::ELF::EM_ARM, LLDB_INVALID_CPUTYPE,
413      0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM
414     {ArchSpec::eCore_arm_aarch64, llvm::ELF::EM_AARCH64, LLDB_INVALID_CPUTYPE,
415      0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM64
416     {ArchSpec::eCore_s390x_generic, llvm::ELF::EM_S390, LLDB_INVALID_CPUTYPE,
417      0xFFFFFFFFu, 0xFFFFFFFFu}, // SystemZ
418     {ArchSpec::eCore_sparc9_generic, llvm::ELF::EM_SPARCV9,
419      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // SPARC V9
420     {ArchSpec::eCore_x86_64_x86_64, llvm::ELF::EM_X86_64, LLDB_INVALID_CPUTYPE,
421      0xFFFFFFFFu, 0xFFFFFFFFu}, // AMD64
422     {ArchSpec::eCore_mips32, llvm::ELF::EM_MIPS, ArchSpec::eMIPSSubType_mips32,
423      0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32
424     {ArchSpec::eCore_mips32r2, llvm::ELF::EM_MIPS,
425      ArchSpec::eMIPSSubType_mips32r2, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r2
426     {ArchSpec::eCore_mips32r6, llvm::ELF::EM_MIPS,
427      ArchSpec::eMIPSSubType_mips32r6, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r6
428     {ArchSpec::eCore_mips32el, llvm::ELF::EM_MIPS,
429      ArchSpec::eMIPSSubType_mips32el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32el
430     {ArchSpec::eCore_mips32r2el, llvm::ELF::EM_MIPS,
431      ArchSpec::eMIPSSubType_mips32r2el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r2el
432     {ArchSpec::eCore_mips32r6el, llvm::ELF::EM_MIPS,
433      ArchSpec::eMIPSSubType_mips32r6el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips32r6el
434     {ArchSpec::eCore_mips64, llvm::ELF::EM_MIPS, ArchSpec::eMIPSSubType_mips64,
435      0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64
436     {ArchSpec::eCore_mips64r2, llvm::ELF::EM_MIPS,
437      ArchSpec::eMIPSSubType_mips64r2, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r2
438     {ArchSpec::eCore_mips64r6, llvm::ELF::EM_MIPS,
439      ArchSpec::eMIPSSubType_mips64r6, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r6
440     {ArchSpec::eCore_mips64el, llvm::ELF::EM_MIPS,
441      ArchSpec::eMIPSSubType_mips64el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64el
442     {ArchSpec::eCore_mips64r2el, llvm::ELF::EM_MIPS,
443      ArchSpec::eMIPSSubType_mips64r2el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r2el
444     {ArchSpec::eCore_mips64r6el, llvm::ELF::EM_MIPS,
445      ArchSpec::eMIPSSubType_mips64r6el, 0xFFFFFFFFu, 0xFFFFFFFFu}, // mips64r6el
446     {ArchSpec::eCore_hexagon_generic, llvm::ELF::EM_HEXAGON,
447      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // HEXAGON
448     {ArchSpec::eCore_kalimba3, llvm::ELF::EM_CSR_KALIMBA,
449      llvm::Triple::KalimbaSubArch_v3, 0xFFFFFFFFu, 0xFFFFFFFFu}, // KALIMBA
450     {ArchSpec::eCore_kalimba4, llvm::ELF::EM_CSR_KALIMBA,
451      llvm::Triple::KalimbaSubArch_v4, 0xFFFFFFFFu, 0xFFFFFFFFu}, // KALIMBA
452     {ArchSpec::eCore_kalimba5, llvm::ELF::EM_CSR_KALIMBA,
453      llvm::Triple::KalimbaSubArch_v5, 0xFFFFFFFFu, 0xFFFFFFFFu} // KALIMBA
454 };
455 
456 static const ArchDefinition g_elf_arch_def = {
457     eArchTypeELF,
458     llvm::array_lengthof(g_elf_arch_entries),
459     g_elf_arch_entries,
460     "elf",
461 };
462 
463 static const ArchDefinitionEntry g_coff_arch_entries[] = {
464     {ArchSpec::eCore_x86_32_i386, llvm::COFF::IMAGE_FILE_MACHINE_I386,
465      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // Intel 80x86
466     {ArchSpec::eCore_ppc_generic, llvm::COFF::IMAGE_FILE_MACHINE_POWERPC,
467      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC
468     {ArchSpec::eCore_ppc_generic, llvm::COFF::IMAGE_FILE_MACHINE_POWERPCFP,
469      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // PowerPC (with FPU)
470     {ArchSpec::eCore_arm_generic, llvm::COFF::IMAGE_FILE_MACHINE_ARM,
471      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARM
472     {ArchSpec::eCore_arm_armv7, llvm::COFF::IMAGE_FILE_MACHINE_ARMNT,
473      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARMv7
474     {ArchSpec::eCore_thumb, llvm::COFF::IMAGE_FILE_MACHINE_THUMB,
475      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu}, // ARMv7
476     {ArchSpec::eCore_x86_64_x86_64, llvm::COFF::IMAGE_FILE_MACHINE_AMD64,
477      LLDB_INVALID_CPUTYPE, 0xFFFFFFFFu, 0xFFFFFFFFu} // AMD64
478 };
479 
480 static const ArchDefinition g_coff_arch_def = {
481     eArchTypeCOFF,
482     llvm::array_lengthof(g_coff_arch_entries),
483     g_coff_arch_entries,
484     "pe-coff",
485 };
486 
487 //===----------------------------------------------------------------------===//
488 // Table of all ArchDefinitions
489 static const ArchDefinition *g_arch_definitions[] = {
490     &g_macho_arch_def, &g_elf_arch_def, &g_coff_arch_def};
491 
492 static const size_t k_num_arch_definitions =
493     llvm::array_lengthof(g_arch_definitions);
494 
495 //===----------------------------------------------------------------------===//
496 // Static helper functions.
497 
498 // Get the architecture definition for a given object type.
499 static const ArchDefinition *FindArchDefinition(ArchitectureType arch_type) {
500   for (unsigned int i = 0; i < k_num_arch_definitions; ++i) {
501     const ArchDefinition *def = g_arch_definitions[i];
502     if (def->type == arch_type)
503       return def;
504   }
505   return nullptr;
506 }
507 
508 // Get an architecture definition by name.
509 static const CoreDefinition *FindCoreDefinition(llvm::StringRef name) {
510   for (unsigned int i = 0; i < llvm::array_lengthof(g_core_definitions); ++i) {
511     if (name.equals_lower(g_core_definitions[i].name))
512       return &g_core_definitions[i];
513   }
514   return nullptr;
515 }
516 
517 static inline const CoreDefinition *FindCoreDefinition(ArchSpec::Core core) {
518   if (core < llvm::array_lengthof(g_core_definitions))
519     return &g_core_definitions[core];
520   return nullptr;
521 }
522 
523 // Get a definition entry by cpu type and subtype.
524 static const ArchDefinitionEntry *
525 FindArchDefinitionEntry(const ArchDefinition *def, uint32_t cpu, uint32_t sub) {
526   if (def == nullptr)
527     return nullptr;
528 
529   const ArchDefinitionEntry *entries = def->entries;
530   for (size_t i = 0; i < def->num_entries; ++i) {
531     if (entries[i].cpu == (cpu & entries[i].cpu_mask))
532       if (entries[i].sub == (sub & entries[i].sub_mask))
533         return &entries[i];
534   }
535   return nullptr;
536 }
537 
538 static const ArchDefinitionEntry *
539 FindArchDefinitionEntry(const ArchDefinition *def, ArchSpec::Core core) {
540   if (def == nullptr)
541     return nullptr;
542 
543   const ArchDefinitionEntry *entries = def->entries;
544   for (size_t i = 0; i < def->num_entries; ++i) {
545     if (entries[i].core == core)
546       return &entries[i];
547   }
548   return nullptr;
549 }
550 
551 //===----------------------------------------------------------------------===//
552 // Constructors and destructors.
553 
554 ArchSpec::ArchSpec() {}
555 
556 ArchSpec::ArchSpec(const char *triple_cstr) {
557   if (triple_cstr)
558     SetTriple(triple_cstr);
559 }
560 
561 ArchSpec::ArchSpec(llvm::StringRef triple_str) { SetTriple(triple_str); }
562 
563 ArchSpec::ArchSpec(const llvm::Triple &triple) { SetTriple(triple); }
564 
565 ArchSpec::ArchSpec(ArchitectureType arch_type, uint32_t cpu, uint32_t subtype) {
566   SetArchitecture(arch_type, cpu, subtype);
567 }
568 
569 ArchSpec::~ArchSpec() = default;
570 
571 //===----------------------------------------------------------------------===//
572 // Assignment and initialization.
573 
574 const ArchSpec &ArchSpec::operator=(const ArchSpec &rhs) {
575   if (this != &rhs) {
576     m_triple = rhs.m_triple;
577     m_core = rhs.m_core;
578     m_byte_order = rhs.m_byte_order;
579     m_distribution_id = rhs.m_distribution_id;
580     m_flags = rhs.m_flags;
581   }
582   return *this;
583 }
584 
585 void ArchSpec::Clear() {
586   m_triple = llvm::Triple();
587   m_core = kCore_invalid;
588   m_byte_order = eByteOrderInvalid;
589   m_distribution_id.Clear();
590   m_flags = 0;
591 }
592 
593 //===----------------------------------------------------------------------===//
594 // Predicates.
595 
596 const char *ArchSpec::GetArchitectureName() const {
597   const CoreDefinition *core_def = FindCoreDefinition(m_core);
598   if (core_def)
599     return core_def->name;
600   return "unknown";
601 }
602 
603 bool ArchSpec::IsMIPS() const {
604   const llvm::Triple::ArchType machine = GetMachine();
605   if (machine == llvm::Triple::mips || machine == llvm::Triple::mipsel ||
606       machine == llvm::Triple::mips64 || machine == llvm::Triple::mips64el)
607     return true;
608   return false;
609 }
610 
611 std::string ArchSpec::GetTargetABI() const {
612 
613   std::string abi;
614 
615   if (IsMIPS()) {
616     switch (GetFlags() & ArchSpec::eMIPSABI_mask) {
617     case ArchSpec::eMIPSABI_N64:
618       abi = "n64";
619       return abi;
620     case ArchSpec::eMIPSABI_N32:
621       abi = "n32";
622       return abi;
623     case ArchSpec::eMIPSABI_O32:
624       abi = "o32";
625       return abi;
626     default:
627       return abi;
628     }
629   }
630   return abi;
631 }
632 
633 void ArchSpec::SetFlags(std::string elf_abi) {
634 
635   uint32_t flag = GetFlags();
636   if (IsMIPS()) {
637     if (elf_abi == "n64")
638       flag |= ArchSpec::eMIPSABI_N64;
639     else if (elf_abi == "n32")
640       flag |= ArchSpec::eMIPSABI_N32;
641     else if (elf_abi == "o32")
642       flag |= ArchSpec::eMIPSABI_O32;
643   }
644   SetFlags(flag);
645 }
646 
647 std::string ArchSpec::GetClangTargetCPU() const {
648   std::string cpu;
649   const llvm::Triple::ArchType machine = GetMachine();
650 
651   if (machine == llvm::Triple::mips || machine == llvm::Triple::mipsel ||
652       machine == llvm::Triple::mips64 || machine == llvm::Triple::mips64el) {
653     switch (m_core) {
654     case ArchSpec::eCore_mips32:
655     case ArchSpec::eCore_mips32el:
656       cpu = "mips32";
657       break;
658     case ArchSpec::eCore_mips32r2:
659     case ArchSpec::eCore_mips32r2el:
660       cpu = "mips32r2";
661       break;
662     case ArchSpec::eCore_mips32r3:
663     case ArchSpec::eCore_mips32r3el:
664       cpu = "mips32r3";
665       break;
666     case ArchSpec::eCore_mips32r5:
667     case ArchSpec::eCore_mips32r5el:
668       cpu = "mips32r5";
669       break;
670     case ArchSpec::eCore_mips32r6:
671     case ArchSpec::eCore_mips32r6el:
672       cpu = "mips32r6";
673       break;
674     case ArchSpec::eCore_mips64:
675     case ArchSpec::eCore_mips64el:
676       cpu = "mips64";
677       break;
678     case ArchSpec::eCore_mips64r2:
679     case ArchSpec::eCore_mips64r2el:
680       cpu = "mips64r2";
681       break;
682     case ArchSpec::eCore_mips64r3:
683     case ArchSpec::eCore_mips64r3el:
684       cpu = "mips64r3";
685       break;
686     case ArchSpec::eCore_mips64r5:
687     case ArchSpec::eCore_mips64r5el:
688       cpu = "mips64r5";
689       break;
690     case ArchSpec::eCore_mips64r6:
691     case ArchSpec::eCore_mips64r6el:
692       cpu = "mips64r6";
693       break;
694     default:
695       break;
696     }
697   }
698   return cpu;
699 }
700 
701 uint32_t ArchSpec::GetMachOCPUType() const {
702   const CoreDefinition *core_def = FindCoreDefinition(m_core);
703   if (core_def) {
704     const ArchDefinitionEntry *arch_def =
705         FindArchDefinitionEntry(&g_macho_arch_def, core_def->core);
706     if (arch_def) {
707       return arch_def->cpu;
708     }
709   }
710   return LLDB_INVALID_CPUTYPE;
711 }
712 
713 uint32_t ArchSpec::GetMachOCPUSubType() const {
714   const CoreDefinition *core_def = FindCoreDefinition(m_core);
715   if (core_def) {
716     const ArchDefinitionEntry *arch_def =
717         FindArchDefinitionEntry(&g_macho_arch_def, core_def->core);
718     if (arch_def) {
719       return arch_def->sub;
720     }
721   }
722   return LLDB_INVALID_CPUTYPE;
723 }
724 
725 uint32_t ArchSpec::GetDataByteSize() const {
726   switch (m_core) {
727   case eCore_kalimba3:
728     return 4;
729   case eCore_kalimba4:
730     return 1;
731   case eCore_kalimba5:
732     return 4;
733   default:
734     return 1;
735   }
736   return 1;
737 }
738 
739 uint32_t ArchSpec::GetCodeByteSize() const {
740   switch (m_core) {
741   case eCore_kalimba3:
742     return 4;
743   case eCore_kalimba4:
744     return 1;
745   case eCore_kalimba5:
746     return 1;
747   default:
748     return 1;
749   }
750   return 1;
751 }
752 
753 llvm::Triple::ArchType ArchSpec::GetMachine() const {
754   const CoreDefinition *core_def = FindCoreDefinition(m_core);
755   if (core_def)
756     return core_def->machine;
757 
758   return llvm::Triple::UnknownArch;
759 }
760 
761 const ConstString &ArchSpec::GetDistributionId() const {
762   return m_distribution_id;
763 }
764 
765 void ArchSpec::SetDistributionId(const char *distribution_id) {
766   m_distribution_id.SetCString(distribution_id);
767 }
768 
769 uint32_t ArchSpec::GetAddressByteSize() const {
770   const CoreDefinition *core_def = FindCoreDefinition(m_core);
771   if (core_def) {
772     if (core_def->machine == llvm::Triple::mips64 ||
773         core_def->machine == llvm::Triple::mips64el) {
774       // For N32/O32 applications Address size is 4 bytes.
775       if (m_flags & (eMIPSABI_N32 | eMIPSABI_O32))
776         return 4;
777     }
778     return core_def->addr_byte_size;
779   }
780   return 0;
781 }
782 
783 ByteOrder ArchSpec::GetDefaultEndian() const {
784   const CoreDefinition *core_def = FindCoreDefinition(m_core);
785   if (core_def)
786     return core_def->default_byte_order;
787   return eByteOrderInvalid;
788 }
789 
790 bool ArchSpec::CharIsSignedByDefault() const {
791   switch (m_triple.getArch()) {
792   default:
793     return true;
794 
795   case llvm::Triple::aarch64:
796   case llvm::Triple::aarch64_be:
797   case llvm::Triple::arm:
798   case llvm::Triple::armeb:
799   case llvm::Triple::thumb:
800   case llvm::Triple::thumbeb:
801     return m_triple.isOSDarwin() || m_triple.isOSWindows();
802 
803   case llvm::Triple::ppc:
804   case llvm::Triple::ppc64:
805     return m_triple.isOSDarwin();
806 
807   case llvm::Triple::ppc64le:
808   case llvm::Triple::systemz:
809   case llvm::Triple::xcore:
810     return false;
811   }
812 }
813 
814 lldb::ByteOrder ArchSpec::GetByteOrder() const {
815   if (m_byte_order == eByteOrderInvalid)
816     return GetDefaultEndian();
817   return m_byte_order;
818 }
819 
820 //===----------------------------------------------------------------------===//
821 // Mutators.
822 
823 bool ArchSpec::SetTriple(const llvm::Triple &triple) {
824   m_triple = triple;
825   UpdateCore();
826   return IsValid();
827 }
828 
829 bool lldb_private::ParseMachCPUDashSubtypeTriple(llvm::StringRef triple_str,
830                                                  ArchSpec &arch) {
831   // Accept "12-10" or "12.10" as cpu type/subtype
832   if (triple_str.empty())
833     return false;
834 
835   size_t pos = triple_str.find_first_of("-.");
836   if (pos == llvm::StringRef::npos)
837     return false;
838 
839   llvm::StringRef cpu_str = triple_str.substr(0, pos);
840   llvm::StringRef remainder = triple_str.substr(pos + 1);
841   if (cpu_str.empty() || remainder.empty())
842     return false;
843 
844   llvm::StringRef sub_str;
845   llvm::StringRef vendor;
846   llvm::StringRef os;
847   std::tie(sub_str, remainder) = remainder.split('-');
848   std::tie(vendor, os) = remainder.split('-');
849 
850   uint32_t cpu = 0;
851   uint32_t sub = 0;
852   if (cpu_str.getAsInteger(10, cpu) || sub_str.getAsInteger(10, sub))
853     return false;
854 
855   if (!arch.SetArchitecture(eArchTypeMachO, cpu, sub))
856     return false;
857   if (!vendor.empty() && !os.empty()) {
858     arch.GetTriple().setVendorName(vendor);
859     arch.GetTriple().setOSName(os);
860   }
861 
862   return true;
863 }
864 
865 bool ArchSpec::SetTriple(llvm::StringRef triple) {
866   if (triple.empty()) {
867     Clear();
868     return false;
869   }
870 
871   if (ParseMachCPUDashSubtypeTriple(triple, *this))
872     return true;
873 
874   SetTriple(llvm::Triple(llvm::Triple::normalize(triple)));
875   return IsValid();
876 }
877 
878 bool ArchSpec::ContainsOnlyArch(const llvm::Triple &normalized_triple) {
879   return !normalized_triple.getArchName().empty() &&
880          normalized_triple.getOSName().empty() &&
881          normalized_triple.getVendorName().empty() &&
882          normalized_triple.getEnvironmentName().empty();
883 }
884 
885 void ArchSpec::MergeFrom(const ArchSpec &other) {
886   if (TripleVendorIsUnspecifiedUnknown() &&
887       !other.TripleVendorIsUnspecifiedUnknown())
888     GetTriple().setVendor(other.GetTriple().getVendor());
889   if (TripleOSIsUnspecifiedUnknown() && !other.TripleOSIsUnspecifiedUnknown())
890     GetTriple().setOS(other.GetTriple().getOS());
891   if (GetTriple().getArch() == llvm::Triple::UnknownArch) {
892     GetTriple().setArch(other.GetTriple().getArch());
893     UpdateCore();
894   }
895   if (GetTriple().getEnvironment() == llvm::Triple::UnknownEnvironment &&
896       !TripleVendorWasSpecified()) {
897     if (other.TripleVendorWasSpecified())
898       GetTriple().setEnvironment(other.GetTriple().getEnvironment());
899   }
900   // If this and other are both arm ArchSpecs and this ArchSpec is a generic
901   // "some kind of arm"
902   // spec but the other ArchSpec is a specific arm core, adopt the specific arm
903   // core.
904   if (GetTriple().getArch() == llvm::Triple::arm &&
905       other.GetTriple().getArch() == llvm::Triple::arm &&
906       IsCompatibleMatch(other) && GetCore() == ArchSpec::eCore_arm_generic &&
907       other.GetCore() != ArchSpec::eCore_arm_generic) {
908     m_core = other.GetCore();
909     CoreUpdated(true);
910   }
911   if (GetFlags() == 0) {
912     SetFlags(other.GetFlags());
913   }
914 }
915 
916 bool ArchSpec::SetArchitecture(ArchitectureType arch_type, uint32_t cpu,
917                                uint32_t sub, uint32_t os) {
918   m_core = kCore_invalid;
919   bool update_triple = true;
920   const ArchDefinition *arch_def = FindArchDefinition(arch_type);
921   if (arch_def) {
922     const ArchDefinitionEntry *arch_def_entry =
923         FindArchDefinitionEntry(arch_def, cpu, sub);
924     if (arch_def_entry) {
925       const CoreDefinition *core_def = FindCoreDefinition(arch_def_entry->core);
926       if (core_def) {
927         m_core = core_def->core;
928         update_triple = false;
929         // Always use the architecture name because it might be more descriptive
930         // than the architecture enum ("armv7" -> llvm::Triple::arm).
931         m_triple.setArchName(llvm::StringRef(core_def->name));
932         if (arch_type == eArchTypeMachO) {
933           m_triple.setVendor(llvm::Triple::Apple);
934 
935           // Don't set the OS.  It could be simulator, macosx, ios, watchos,
936           // tvos.  We could
937           // get close with the cpu type - but we can't get it right all of the
938           // time.  Better
939           // to leave this unset so other sections of code will set it when they
940           // have more
941           // information.
942           // NB: don't call m_triple.setOS (llvm::Triple::UnknownOS).  That sets
943           // the OSName to
944           // "unknown" and the ArchSpec::TripleVendorWasSpecified() method says
945           // that any
946           // OSName setting means it was specified.
947         } else if (arch_type == eArchTypeELF) {
948           switch (os) {
949           case llvm::ELF::ELFOSABI_AIX:
950             m_triple.setOS(llvm::Triple::OSType::AIX);
951             break;
952           case llvm::ELF::ELFOSABI_FREEBSD:
953             m_triple.setOS(llvm::Triple::OSType::FreeBSD);
954             break;
955           case llvm::ELF::ELFOSABI_GNU:
956             m_triple.setOS(llvm::Triple::OSType::Linux);
957             break;
958           case llvm::ELF::ELFOSABI_NETBSD:
959             m_triple.setOS(llvm::Triple::OSType::NetBSD);
960             break;
961           case llvm::ELF::ELFOSABI_OPENBSD:
962             m_triple.setOS(llvm::Triple::OSType::OpenBSD);
963             break;
964           case llvm::ELF::ELFOSABI_SOLARIS:
965             m_triple.setOS(llvm::Triple::OSType::Solaris);
966             break;
967           }
968         } else if (arch_type == eArchTypeCOFF && os == llvm::Triple::Win32) {
969           m_triple.setVendor(llvm::Triple::PC);
970           m_triple.setOS(llvm::Triple::Win32);
971         } else {
972           m_triple.setVendor(llvm::Triple::UnknownVendor);
973           m_triple.setOS(llvm::Triple::UnknownOS);
974         }
975         // Fall back onto setting the machine type if the arch by name failed...
976         if (m_triple.getArch() == llvm::Triple::UnknownArch)
977           m_triple.setArch(core_def->machine);
978       }
979     }
980   }
981   CoreUpdated(update_triple);
982   return IsValid();
983 }
984 
985 uint32_t ArchSpec::GetMinimumOpcodeByteSize() const {
986   const CoreDefinition *core_def = FindCoreDefinition(m_core);
987   if (core_def)
988     return core_def->min_opcode_byte_size;
989   return 0;
990 }
991 
992 uint32_t ArchSpec::GetMaximumOpcodeByteSize() const {
993   const CoreDefinition *core_def = FindCoreDefinition(m_core);
994   if (core_def)
995     return core_def->max_opcode_byte_size;
996   return 0;
997 }
998 
999 bool ArchSpec::IsExactMatch(const ArchSpec &rhs) const {
1000   return IsEqualTo(rhs, true);
1001 }
1002 
1003 bool ArchSpec::IsCompatibleMatch(const ArchSpec &rhs) const {
1004   return IsEqualTo(rhs, false);
1005 }
1006 
1007 static bool isCompatibleEnvironment(llvm::Triple::EnvironmentType lhs,
1008                                     llvm::Triple::EnvironmentType rhs) {
1009   if (lhs == rhs)
1010     return true;
1011 
1012   // If any of the environment is unknown then they are compatible
1013   if (lhs == llvm::Triple::UnknownEnvironment ||
1014       rhs == llvm::Triple::UnknownEnvironment)
1015     return true;
1016 
1017   // If one of the environment is Android and the other one is EABI then they
1018   // are considered to
1019   // be compatible. This is required as a workaround for shared libraries
1020   // compiled for Android
1021   // without the NOTE section indicating that they are using the Android ABI.
1022   if ((lhs == llvm::Triple::Android && rhs == llvm::Triple::EABI) ||
1023       (rhs == llvm::Triple::Android && lhs == llvm::Triple::EABI) ||
1024       (lhs == llvm::Triple::GNUEABI && rhs == llvm::Triple::EABI) ||
1025       (rhs == llvm::Triple::GNUEABI && lhs == llvm::Triple::EABI) ||
1026       (lhs == llvm::Triple::GNUEABIHF && rhs == llvm::Triple::EABIHF) ||
1027       (rhs == llvm::Triple::GNUEABIHF && lhs == llvm::Triple::EABIHF))
1028     return true;
1029 
1030   return false;
1031 }
1032 
1033 bool ArchSpec::IsEqualTo(const ArchSpec &rhs, bool exact_match) const {
1034   // explicitly ignoring m_distribution_id in this method.
1035 
1036   if (GetByteOrder() != rhs.GetByteOrder())
1037     return false;
1038 
1039   const ArchSpec::Core lhs_core = GetCore();
1040   const ArchSpec::Core rhs_core = rhs.GetCore();
1041 
1042   const bool core_match = cores_match(lhs_core, rhs_core, true, exact_match);
1043 
1044   if (core_match) {
1045     const llvm::Triple &lhs_triple = GetTriple();
1046     const llvm::Triple &rhs_triple = rhs.GetTriple();
1047 
1048     const llvm::Triple::VendorType lhs_triple_vendor = lhs_triple.getVendor();
1049     const llvm::Triple::VendorType rhs_triple_vendor = rhs_triple.getVendor();
1050     if (lhs_triple_vendor != rhs_triple_vendor) {
1051       const bool rhs_vendor_specified = rhs.TripleVendorWasSpecified();
1052       const bool lhs_vendor_specified = TripleVendorWasSpecified();
1053       // Both architectures had the vendor specified, so if they aren't
1054       // equal then we return false
1055       if (rhs_vendor_specified && lhs_vendor_specified)
1056         return false;
1057 
1058       // Only fail if both vendor types are not unknown
1059       if (lhs_triple_vendor != llvm::Triple::UnknownVendor &&
1060           rhs_triple_vendor != llvm::Triple::UnknownVendor)
1061         return false;
1062     }
1063 
1064     const llvm::Triple::OSType lhs_triple_os = lhs_triple.getOS();
1065     const llvm::Triple::OSType rhs_triple_os = rhs_triple.getOS();
1066     if (lhs_triple_os != rhs_triple_os) {
1067       const bool rhs_os_specified = rhs.TripleOSWasSpecified();
1068       const bool lhs_os_specified = TripleOSWasSpecified();
1069       // Both architectures had the OS specified, so if they aren't
1070       // equal then we return false
1071       if (rhs_os_specified && lhs_os_specified)
1072         return false;
1073 
1074       // Only fail if both os types are not unknown
1075       if (lhs_triple_os != llvm::Triple::UnknownOS &&
1076           rhs_triple_os != llvm::Triple::UnknownOS)
1077         return false;
1078     }
1079 
1080     const llvm::Triple::EnvironmentType lhs_triple_env =
1081         lhs_triple.getEnvironment();
1082     const llvm::Triple::EnvironmentType rhs_triple_env =
1083         rhs_triple.getEnvironment();
1084 
1085     if (!isCompatibleEnvironment(lhs_triple_env, rhs_triple_env))
1086       return false;
1087     return true;
1088   }
1089   return false;
1090 }
1091 
1092 void ArchSpec::UpdateCore() {
1093   llvm::StringRef arch_name(m_triple.getArchName());
1094   const CoreDefinition *core_def = FindCoreDefinition(arch_name);
1095   if (core_def) {
1096     m_core = core_def->core;
1097     // Set the byte order to the default byte order for an architecture.
1098     // This can be modified if needed for cases when cores handle both
1099     // big and little endian
1100     m_byte_order = core_def->default_byte_order;
1101   } else {
1102     Clear();
1103   }
1104 }
1105 
1106 //===----------------------------------------------------------------------===//
1107 // Helper methods.
1108 
1109 void ArchSpec::CoreUpdated(bool update_triple) {
1110   const CoreDefinition *core_def = FindCoreDefinition(m_core);
1111   if (core_def) {
1112     if (update_triple)
1113       m_triple = llvm::Triple(core_def->name, "unknown", "unknown");
1114     m_byte_order = core_def->default_byte_order;
1115   } else {
1116     if (update_triple)
1117       m_triple = llvm::Triple();
1118     m_byte_order = eByteOrderInvalid;
1119   }
1120 }
1121 
1122 //===----------------------------------------------------------------------===//
1123 // Operators.
1124 
1125 static bool cores_match(const ArchSpec::Core core1, const ArchSpec::Core core2,
1126                         bool try_inverse, bool enforce_exact_match) {
1127   if (core1 == core2)
1128     return true;
1129 
1130   switch (core1) {
1131   case ArchSpec::kCore_any:
1132     return true;
1133 
1134   case ArchSpec::eCore_arm_generic:
1135     if (enforce_exact_match)
1136       break;
1137     LLVM_FALLTHROUGH;
1138   case ArchSpec::kCore_arm_any:
1139     if (core2 >= ArchSpec::kCore_arm_first && core2 <= ArchSpec::kCore_arm_last)
1140       return true;
1141     if (core2 >= ArchSpec::kCore_thumb_first &&
1142         core2 <= ArchSpec::kCore_thumb_last)
1143       return true;
1144     if (core2 == ArchSpec::kCore_arm_any)
1145       return true;
1146     break;
1147 
1148   case ArchSpec::kCore_x86_32_any:
1149     if ((core2 >= ArchSpec::kCore_x86_32_first &&
1150          core2 <= ArchSpec::kCore_x86_32_last) ||
1151         (core2 == ArchSpec::kCore_x86_32_any))
1152       return true;
1153     break;
1154 
1155   case ArchSpec::kCore_x86_64_any:
1156     if ((core2 >= ArchSpec::kCore_x86_64_first &&
1157          core2 <= ArchSpec::kCore_x86_64_last) ||
1158         (core2 == ArchSpec::kCore_x86_64_any))
1159       return true;
1160     break;
1161 
1162   case ArchSpec::kCore_ppc_any:
1163     if ((core2 >= ArchSpec::kCore_ppc_first &&
1164          core2 <= ArchSpec::kCore_ppc_last) ||
1165         (core2 == ArchSpec::kCore_ppc_any))
1166       return true;
1167     break;
1168 
1169   case ArchSpec::kCore_ppc64_any:
1170     if ((core2 >= ArchSpec::kCore_ppc64_first &&
1171          core2 <= ArchSpec::kCore_ppc64_last) ||
1172         (core2 == ArchSpec::kCore_ppc64_any))
1173       return true;
1174     break;
1175 
1176   case ArchSpec::eCore_arm_armv6m:
1177     if (!enforce_exact_match) {
1178       if (core2 == ArchSpec::eCore_arm_generic)
1179         return true;
1180       try_inverse = false;
1181       if (core2 == ArchSpec::eCore_arm_armv7)
1182         return true;
1183       if (core2 == ArchSpec::eCore_arm_armv6m)
1184         return true;
1185     }
1186     break;
1187 
1188   case ArchSpec::kCore_hexagon_any:
1189     if ((core2 >= ArchSpec::kCore_hexagon_first &&
1190          core2 <= ArchSpec::kCore_hexagon_last) ||
1191         (core2 == ArchSpec::kCore_hexagon_any))
1192       return true;
1193     break;
1194 
1195   // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1196   // Cortex-M0 - ARMv6-M - armv6m
1197   // Cortex-M3 - ARMv7-M - armv7m
1198   // Cortex-M4 - ARMv7E-M - armv7em
1199   case ArchSpec::eCore_arm_armv7em:
1200     if (!enforce_exact_match) {
1201       if (core2 == ArchSpec::eCore_arm_generic)
1202         return true;
1203       if (core2 == ArchSpec::eCore_arm_armv7m)
1204         return true;
1205       if (core2 == ArchSpec::eCore_arm_armv6m)
1206         return true;
1207       if (core2 == ArchSpec::eCore_arm_armv7)
1208         return true;
1209       try_inverse = true;
1210     }
1211     break;
1212 
1213   // v. https://en.wikipedia.org/wiki/ARM_Cortex-M#Silicon_customization
1214   // Cortex-M0 - ARMv6-M - armv6m
1215   // Cortex-M3 - ARMv7-M - armv7m
1216   // Cortex-M4 - ARMv7E-M - armv7em
1217   case ArchSpec::eCore_arm_armv7m:
1218     if (!enforce_exact_match) {
1219       if (core2 == ArchSpec::eCore_arm_generic)
1220         return true;
1221       if (core2 == ArchSpec::eCore_arm_armv6m)
1222         return true;
1223       if (core2 == ArchSpec::eCore_arm_armv7)
1224         return true;
1225       if (core2 == ArchSpec::eCore_arm_armv7em)
1226         return true;
1227       try_inverse = true;
1228     }
1229     break;
1230 
1231   case ArchSpec::eCore_arm_armv7f:
1232   case ArchSpec::eCore_arm_armv7k:
1233   case ArchSpec::eCore_arm_armv7s:
1234     if (!enforce_exact_match) {
1235       if (core2 == ArchSpec::eCore_arm_generic)
1236         return true;
1237       if (core2 == ArchSpec::eCore_arm_armv7)
1238         return true;
1239       try_inverse = false;
1240     }
1241     break;
1242 
1243   case ArchSpec::eCore_x86_64_x86_64h:
1244     if (!enforce_exact_match) {
1245       try_inverse = false;
1246       if (core2 == ArchSpec::eCore_x86_64_x86_64)
1247         return true;
1248     }
1249     break;
1250 
1251   case ArchSpec::eCore_arm_armv8:
1252     if (!enforce_exact_match) {
1253       if (core2 == ArchSpec::eCore_arm_arm64)
1254         return true;
1255       if (core2 == ArchSpec::eCore_arm_aarch64)
1256         return true;
1257       try_inverse = false;
1258     }
1259     break;
1260 
1261   case ArchSpec::eCore_arm_aarch64:
1262     if (!enforce_exact_match) {
1263       if (core2 == ArchSpec::eCore_arm_arm64)
1264         return true;
1265       if (core2 == ArchSpec::eCore_arm_armv8)
1266         return true;
1267       try_inverse = false;
1268     }
1269     break;
1270 
1271   case ArchSpec::eCore_arm_arm64:
1272     if (!enforce_exact_match) {
1273       if (core2 == ArchSpec::eCore_arm_aarch64)
1274         return true;
1275       if (core2 == ArchSpec::eCore_arm_armv8)
1276         return true;
1277       try_inverse = false;
1278     }
1279     break;
1280 
1281   case ArchSpec::eCore_mips32:
1282     if (!enforce_exact_match) {
1283       if (core2 >= ArchSpec::kCore_mips32_first &&
1284           core2 <= ArchSpec::kCore_mips32_last)
1285         return true;
1286       try_inverse = false;
1287     }
1288     break;
1289 
1290   case ArchSpec::eCore_mips32el:
1291     if (!enforce_exact_match) {
1292       if (core2 >= ArchSpec::kCore_mips32el_first &&
1293           core2 <= ArchSpec::kCore_mips32el_last)
1294         return true;
1295       try_inverse = true;
1296     }
1297     break;
1298 
1299   case ArchSpec::eCore_mips64:
1300     if (!enforce_exact_match) {
1301       if (core2 >= ArchSpec::kCore_mips32_first &&
1302           core2 <= ArchSpec::kCore_mips32_last)
1303         return true;
1304       if (core2 >= ArchSpec::kCore_mips64_first &&
1305           core2 <= ArchSpec::kCore_mips64_last)
1306         return true;
1307       try_inverse = false;
1308     }
1309     break;
1310 
1311   case ArchSpec::eCore_mips64el:
1312     if (!enforce_exact_match) {
1313       if (core2 >= ArchSpec::kCore_mips32el_first &&
1314           core2 <= ArchSpec::kCore_mips32el_last)
1315         return true;
1316       if (core2 >= ArchSpec::kCore_mips64el_first &&
1317           core2 <= ArchSpec::kCore_mips64el_last)
1318         return true;
1319       try_inverse = false;
1320     }
1321     break;
1322 
1323   case ArchSpec::eCore_mips64r2:
1324   case ArchSpec::eCore_mips64r3:
1325   case ArchSpec::eCore_mips64r5:
1326     if (!enforce_exact_match) {
1327       if (core2 >= ArchSpec::kCore_mips32_first && core2 <= (core1 - 10))
1328         return true;
1329       if (core2 >= ArchSpec::kCore_mips64_first && core2 <= (core1 - 1))
1330         return true;
1331       try_inverse = false;
1332     }
1333     break;
1334 
1335   case ArchSpec::eCore_mips64r2el:
1336   case ArchSpec::eCore_mips64r3el:
1337   case ArchSpec::eCore_mips64r5el:
1338     if (!enforce_exact_match) {
1339       if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= (core1 - 10))
1340         return true;
1341       if (core2 >= ArchSpec::kCore_mips64el_first && core2 <= (core1 - 1))
1342         return true;
1343       try_inverse = false;
1344     }
1345     break;
1346 
1347   case ArchSpec::eCore_mips32r2:
1348   case ArchSpec::eCore_mips32r3:
1349   case ArchSpec::eCore_mips32r5:
1350     if (!enforce_exact_match) {
1351       if (core2 >= ArchSpec::kCore_mips32_first && core2 <= core1)
1352         return true;
1353     }
1354     break;
1355 
1356   case ArchSpec::eCore_mips32r2el:
1357   case ArchSpec::eCore_mips32r3el:
1358   case ArchSpec::eCore_mips32r5el:
1359     if (!enforce_exact_match) {
1360       if (core2 >= ArchSpec::kCore_mips32el_first && core2 <= core1)
1361         return true;
1362     }
1363     break;
1364 
1365   case ArchSpec::eCore_mips32r6:
1366     if (!enforce_exact_match) {
1367       if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1368         return true;
1369     }
1370     break;
1371 
1372   case ArchSpec::eCore_mips32r6el:
1373     if (!enforce_exact_match) {
1374       if (core2 == ArchSpec::eCore_mips32el ||
1375           core2 == ArchSpec::eCore_mips32r6el)
1376         return true;
1377     }
1378     break;
1379 
1380   case ArchSpec::eCore_mips64r6:
1381     if (!enforce_exact_match) {
1382       if (core2 == ArchSpec::eCore_mips32 || core2 == ArchSpec::eCore_mips32r6)
1383         return true;
1384       if (core2 == ArchSpec::eCore_mips64 || core2 == ArchSpec::eCore_mips64r6)
1385         return true;
1386     }
1387     break;
1388 
1389   case ArchSpec::eCore_mips64r6el:
1390     if (!enforce_exact_match) {
1391       if (core2 == ArchSpec::eCore_mips32el ||
1392           core2 == ArchSpec::eCore_mips32r6el)
1393         return true;
1394       if (core2 == ArchSpec::eCore_mips64el ||
1395           core2 == ArchSpec::eCore_mips64r6el)
1396         return true;
1397     }
1398     break;
1399 
1400   default:
1401     break;
1402   }
1403   if (try_inverse)
1404     return cores_match(core2, core1, false, enforce_exact_match);
1405   return false;
1406 }
1407 
1408 bool lldb_private::operator<(const ArchSpec &lhs, const ArchSpec &rhs) {
1409   const ArchSpec::Core lhs_core = lhs.GetCore();
1410   const ArchSpec::Core rhs_core = rhs.GetCore();
1411   return lhs_core < rhs_core;
1412 }
1413 
1414 bool ArchSpec::IsFullySpecifiedTriple() const {
1415   const auto &user_specified_triple = GetTriple();
1416 
1417   bool user_triple_fully_specified = false;
1418 
1419   if ((user_specified_triple.getOS() != llvm::Triple::UnknownOS) ||
1420       TripleOSWasSpecified()) {
1421     if ((user_specified_triple.getVendor() != llvm::Triple::UnknownVendor) ||
1422         TripleVendorWasSpecified()) {
1423       const unsigned unspecified = 0;
1424       if (user_specified_triple.getOSMajorVersion() != unspecified) {
1425         user_triple_fully_specified = true;
1426       }
1427     }
1428   }
1429 
1430   return user_triple_fully_specified;
1431 }
1432 
1433 void ArchSpec::PiecewiseTripleCompare(
1434     const ArchSpec &other, bool &arch_different, bool &vendor_different,
1435     bool &os_different, bool &os_version_different, bool &env_different) const {
1436   const llvm::Triple &me(GetTriple());
1437   const llvm::Triple &them(other.GetTriple());
1438 
1439   arch_different = (me.getArch() != them.getArch());
1440 
1441   vendor_different = (me.getVendor() != them.getVendor());
1442 
1443   os_different = (me.getOS() != them.getOS());
1444 
1445   os_version_different = (me.getOSMajorVersion() != them.getOSMajorVersion());
1446 
1447   env_different = (me.getEnvironment() != them.getEnvironment());
1448 }
1449 
1450 bool ArchSpec::IsAlwaysThumbInstructions() const {
1451   std::string Status;
1452   if (GetTriple().getArch() == llvm::Triple::arm ||
1453       GetTriple().getArch() == llvm::Triple::thumb) {
1454     // v. https://en.wikipedia.org/wiki/ARM_Cortex-M
1455     //
1456     // Cortex-M0 through Cortex-M7 are ARM processor cores which can only
1457     // execute thumb instructions.  We map the cores to arch names like this:
1458     //
1459     // Cortex-M0, Cortex-M0+, Cortex-M1:  armv6m
1460     // Cortex-M3: armv7m
1461     // Cortex-M4, Cortex-M7: armv7em
1462 
1463     if (GetCore() == ArchSpec::Core::eCore_arm_armv7m ||
1464         GetCore() == ArchSpec::Core::eCore_arm_armv7em ||
1465         GetCore() == ArchSpec::Core::eCore_arm_armv6m) {
1466       return true;
1467     }
1468   }
1469   return false;
1470 }
1471 
1472 void ArchSpec::DumpTriple(Stream &s) const {
1473   const llvm::Triple &triple = GetTriple();
1474   llvm::StringRef arch_str = triple.getArchName();
1475   llvm::StringRef vendor_str = triple.getVendorName();
1476   llvm::StringRef os_str = triple.getOSName();
1477   llvm::StringRef environ_str = triple.getEnvironmentName();
1478 
1479   s.Printf("%s-%s-%s", arch_str.empty() ? "*" : arch_str.str().c_str(),
1480            vendor_str.empty() ? "*" : vendor_str.str().c_str(),
1481            os_str.empty() ? "*" : os_str.str().c_str());
1482 
1483   if (!environ_str.empty())
1484     s.Printf("-%s", environ_str.str().c_str());
1485 }
1486