1 // Copyright 2012 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h> // Must come first
31 #endif
32
33 #include "common/mac/arch_utilities.h"
34
35 #include <mach/machine.h>
36 #include <mach-o/arch.h>
37 #include <mach-o/fat.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #ifdef __APPLE__
42 #include <mach-o/utils.h>
43 #endif
44
45 namespace {
46
47 enum Architecture {
48 kArch_i386 = 0,
49 kArch_x86_64,
50 kArch_x86_64h,
51 kArch_arm,
52 kArch_arm64,
53 kArch_arm64e,
54 kArch_ppc,
55 // This must be last.
56 kNumArchitectures
57 };
58
59 struct NamedArchInfo {
60 const char* name;
61 ArchInfo info;
62 };
63
64 // enum Architecture above and kKnownArchitectures below
65 // must be kept in sync.
66 constexpr NamedArchInfo kKnownArchitectures[] = {
67 {"i386", {CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL}},
68 {"x86_64", {CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL}},
69 {"x86_64h", {CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_H}},
70 {"arm", {CPU_TYPE_ARM, CPU_SUBTYPE_ARM_ALL}},
71 {"arm64", {CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64_ALL}},
72 {"arm64e", {CPU_TYPE_ARM64, CPU_SUBTYPE_ARM64E}},
73 {"ppc", {CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL}}};
74
75 } // namespace
76
GetLocalArchInfo(void)77 ArchInfo GetLocalArchInfo(void) {
78 Architecture arch;
79 #if defined(__i386__)
80 arch = kArch_i386;
81 #elif defined(__x86_64__)
82 arch = kArch_x86_64;
83 #elif defined(__arm64__) || defined(__aarch64__)
84 arch = kArch_arm64;
85 #elif defined(__arm__)
86 arch = kArch_arm;
87 #elif defined(__powerpc__)
88 arch = kArch_ppc;
89 #else
90 #error "Unsupported CPU architecture"
91 #endif
92 return kKnownArchitectures[arch].info;
93 }
94
95 #ifdef __APPLE__
96
GetArchInfoFromName(const char * arch_name)97 std::optional<ArchInfo> GetArchInfoFromName(const char* arch_name) {
98 if (__builtin_available(macOS 13.0, iOS 16.0, *)) {
99 cpu_type_t type;
100 cpu_subtype_t subtype;
101 if (macho_cpu_type_for_arch_name(arch_name, &type, &subtype)) {
102 return ArchInfo{type, subtype};
103 }
104 } else {
105 #pragma clang diagnostic push
106 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
107 const NXArchInfo* info = NXGetArchInfoFromName(arch_name);
108 #pragma clang diagnostic pop
109 if (info) {
110 return ArchInfo{info->cputype, info->cpusubtype};
111 }
112 }
113 return std::nullopt;
114 }
115
GetNameFromCPUType(cpu_type_t cpu_type,cpu_subtype_t cpu_subtype)116 const char* GetNameFromCPUType(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) {
117 if (__builtin_available(macOS 13.0, iOS 16.0, *)) {
118 const char* name = macho_arch_name_for_cpu_type(cpu_type, cpu_subtype);
119 if (name) {
120 return name;
121 }
122 } else {
123 #pragma clang diagnostic push
124 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
125 const NXArchInfo* info = NXGetArchInfoFromCpuType(cpu_type, cpu_subtype);
126 #pragma clang diagnostic pop
127 if (info) {
128 return info->name;
129 }
130 }
131
132 return kUnknownArchName;
133 }
134
135 #else
136
GetArchInfoFromName(const char * arch_name)137 std::optional<ArchInfo> GetArchInfoFromName(const char* arch_name) {
138 for (int arch = 0; arch < kNumArchitectures; ++arch) {
139 if (!strcmp(arch_name, kKnownArchitectures[arch].name)) {
140 return kKnownArchitectures[arch].info;
141 }
142 }
143 return std::nullopt;
144 }
145
GetNameFromCPUType(cpu_type_t cpu_type,cpu_subtype_t cpu_subtype)146 const char* GetNameFromCPUType(cpu_type_t cpu_type, cpu_subtype_t cpu_subtype) {
147 const char* candidate = kUnknownArchName;
148 for (int arch = 0; arch < kNumArchitectures; ++arch) {
149 if (kKnownArchitectures[arch].info.cputype == cpu_type) {
150 if (kKnownArchitectures[arch].info.cpusubtype == cpu_subtype) {
151 return kKnownArchitectures[arch].name;
152 }
153 if (!strcmp(candidate, kUnknownArchName)) {
154 candidate = kKnownArchitectures[arch].name;
155 }
156 }
157 }
158 return candidate;
159 }
160 #endif // __APPLE__
161