@@ -4,6 +4,10 @@ using namespace BinaryNinja;
44using namespace std ;
55
66
7+ Ref<Platform> g_macX86, g_macX64, g_macArmv7, g_macThumb2, g_macArm64;
8+ Ref<Platform> g_iosArmv7, g_iosThumb2, g_iosArm64;
9+
10+
711class MacX86Platform : public Platform
812{
913public:
@@ -31,6 +35,17 @@ class MacX86Platform: public Platform
3135 {
3236 return false ;
3337 }
38+
39+ static Ref<Platform> Recognize (BinaryView* view, Metadata* metadata)
40+ {
41+ auto machoPlatform = metadata->Get (" machoplatform" );
42+ if (!machoPlatform || !machoPlatform->IsUnsignedInteger ())
43+ return nullptr ;
44+ if (machoPlatform->GetUnsignedInteger () != 2 )
45+ return g_macX86;
46+
47+ return nullptr ;
48+ }
3449};
3550
3651
@@ -55,6 +70,17 @@ class MacX64Platform: public Platform
5570 {
5671 return false ;
5772 }
73+
74+ static Ref<Platform> Recognize (BinaryView* view, Metadata* metadata)
75+ {
76+ auto machoPlatform = metadata->Get (" machoplatform" );
77+ if (!machoPlatform || !machoPlatform->IsUnsignedInteger ())
78+ return nullptr ;
79+ if (machoPlatform->GetUnsignedInteger () != 2 )
80+ return g_macX64;
81+
82+ return nullptr ;
83+ }
5884};
5985
6086
@@ -79,6 +105,17 @@ class MacArmv7Platform: public Platform
79105 {
80106 return false ;
81107 }
108+
109+ static Ref<Platform> Recognize (BinaryView* view, Metadata* metadata)
110+ {
111+ auto machoPlatform = metadata->Get (" machoplatform" );
112+ if (!machoPlatform || !machoPlatform->IsUnsignedInteger ())
113+ return nullptr ;
114+ if (machoPlatform->GetUnsignedInteger () != 2 )
115+ return g_macArmv7;
116+
117+ return nullptr ;
118+ }
82119};
83120
84121
@@ -103,6 +140,86 @@ class MacArm64Platform: public Platform
103140 {
104141 return false ;
105142 }
143+
144+ static Ref<Platform> Recognize (BinaryView* view, Metadata* metadata)
145+ {
146+ auto machoPlatform = metadata->Get (" machoplatform" );
147+ if (!machoPlatform || !machoPlatform->IsUnsignedInteger ())
148+ return nullptr ;
149+ if (machoPlatform->GetUnsignedInteger () != 2 )
150+ return g_macArm64;
151+
152+ return nullptr ;
153+ }
154+ };
155+
156+
157+ class IOSArmv7Platform : public Platform
158+ {
159+ public:
160+ IOSArmv7Platform (Architecture* arch, const std::string& name): Platform(arch, name)
161+ {
162+ Ref<CallingConvention> cc;
163+
164+ cc = arch->GetCallingConventionByName (" cdecl" );
165+ if (cc)
166+ {
167+ RegisterDefaultCallingConvention (cc);
168+ RegisterCdeclCallingConvention (cc);
169+ RegisterFastcallCallingConvention (cc);
170+ RegisterStdcallCallingConvention (cc);
171+ }
172+ }
173+
174+ virtual bool GetFallbackEnabled () override
175+ {
176+ return false ;
177+ }
178+
179+ static Ref<Platform> Recognize (BinaryView* view, Metadata* metadata)
180+ {
181+ auto machoPlatform = metadata->Get (" machoplatform" );
182+ if (!machoPlatform || !machoPlatform->IsUnsignedInteger ())
183+ return nullptr ;
184+ if (machoPlatform->GetUnsignedInteger () == 2 )
185+ return g_iosArmv7;
186+
187+ return nullptr ;
188+ }
189+ };
190+
191+ class IOSArm64Platform : public Platform
192+ {
193+ public:
194+ IOSArm64Platform (Architecture* arch): Platform(arch, " ios-aarch64" )
195+ {
196+ Ref<CallingConvention> cc;
197+
198+ cc = arch->GetCallingConventionByName (" apple-arm64" );
199+ if (cc)
200+ {
201+ RegisterDefaultCallingConvention (cc);
202+ RegisterCdeclCallingConvention (cc);
203+ RegisterFastcallCallingConvention (cc);
204+ RegisterStdcallCallingConvention (cc);
205+ }
206+ }
207+
208+ virtual bool GetFallbackEnabled () override
209+ {
210+ return false ;
211+ }
212+
213+ static Ref<Platform> Recognize (BinaryView* view, Metadata* metadata)
214+ {
215+ auto machoPlatform = metadata->Get (" machoplatform" );
216+ if (!machoPlatform || !machoPlatform->IsUnsignedInteger ())
217+ return nullptr ;
218+ if (machoPlatform->GetUnsignedInteger () == 2 )
219+ return g_iosArm64;
220+
221+ return nullptr ;
222+ }
106223};
107224
108225
@@ -126,50 +243,55 @@ extern "C"
126243 BINARYNINJAPLUGIN bool CorePluginInit ()
127244#endif
128245 {
246+ auto viewType = BinaryViewType::GetByName (" Mach-O" );
129247 Ref<Architecture> x86 = Architecture::GetByName (" x86" );
130248 if (x86)
131249 {
132- Ref<Platform> platform;
133-
134- platform = new MacX86Platform (x86);
135- Platform::Register (" mac" , platform);
136- BinaryViewType::RegisterPlatform (" Mach-O" , 0 , x86, platform);
250+ g_macX86 = new MacX86Platform (x86);
251+ Platform::Register (" mac" , g_macX86);
252+ viewType->RegisterPlatformRecognizer (7 , LittleEndian, MacX86Platform::Recognize);
137253 }
138254
139255 Ref<Architecture> x64 = Architecture::GetByName (" x86_64" );
140256 if (x64)
141257 {
142- Ref<Platform> platform;
143-
144- platform = new MacX64Platform (x64);
145- Platform::Register (" mac" , platform);
146- BinaryViewType::RegisterPlatform (" Mach-O" , 0 , x64, platform);
258+ g_macX64 = new MacX64Platform (x64);
259+ Platform::Register (" mac" , g_macX64);
260+ viewType->RegisterPlatformRecognizer (0x01000007 , LittleEndian, MacX64Platform::Recognize);
147261 }
148262
149263 Ref<Architecture> armv7 = Architecture::GetByName (" armv7" );
150264 Ref<Architecture> thumb2 = Architecture::GetByName (" thumb2" );
151265 if (armv7 && thumb2)
152266 {
153- Ref<Platform> armPlatform, thumbPlatform;
154-
155- armPlatform = new MacArmv7Platform (armv7, " mac-armv7" );
156- thumbPlatform = new MacArmv7Platform (thumb2, " mac-thumb2" );
157- armPlatform->AddRelatedPlatform (thumb2, thumbPlatform);
158- thumbPlatform->AddRelatedPlatform (armv7, armPlatform);
159- Platform::Register (" mac" , armPlatform);
160- Platform::Register (" mac" , thumbPlatform);
161- BinaryViewType::RegisterPlatform (" Mach-O" , 0 , armv7, armPlatform);
267+ g_macArmv7 = new MacArmv7Platform (armv7, " mac-armv7" );
268+ g_macThumb2 = new MacArmv7Platform (thumb2, " mac-thumb2" );
269+ g_iosArmv7 = new IOSArmv7Platform (armv7, " ios-armv7" );
270+ g_iosThumb2 = new IOSArmv7Platform (thumb2, " ios-thumb2" );
271+ g_macArmv7->AddRelatedPlatform (thumb2, g_macThumb2);
272+ g_macThumb2->AddRelatedPlatform (armv7, g_macArmv7);
273+ g_iosArmv7->AddRelatedPlatform (thumb2, g_iosThumb2);
274+ g_iosThumb2->AddRelatedPlatform (armv7, g_iosArmv7);
275+ Platform::Register (" mac" , g_macArmv7);
276+ Platform::Register (" ios" , g_iosArmv7);
277+ Platform::Register (" mac" , g_macThumb2);
278+ Platform::Register (" ios" , g_iosThumb2);
279+ viewType->RegisterPlatformRecognizer (0xc , LittleEndian, MacArmv7Platform::Recognize);
280+ viewType->RegisterPlatformRecognizer (0xc , LittleEndian, IOSArmv7Platform::Recognize);
162281 }
163282
164283 Ref<Architecture> arm64 = Architecture::GetByName (" aarch64" );
165284 if (arm64)
166285 {
167- Ref<Platform> platform;
168-
169- platform = new MacArm64Platform (arm64);
170- Platform::Register (" mac" , platform);
171- BinaryViewType::RegisterPlatform (" Mach-O" , 9 , arm64, platform);
172- BinaryViewType::RegisterPlatform (" Mach-O" , 0 , arm64, platform);
286+ g_macArm64 = new MacArm64Platform (arm64);
287+ g_iosArm64 = new IOSArm64Platform (arm64);
288+ Platform::Register (" mac" , g_macArm64);
289+ Platform::Register (" ios" , g_iosArm64);
290+ viewType->RegisterPlatformRecognizer (0 , LittleEndian, MacArm64Platform::Recognize);
291+ viewType->RegisterPlatformRecognizer (0x0100000c , LittleEndian, MacArm64Platform::Recognize);
292+ viewType->RegisterPlatformRecognizer (0x0200000c , LittleEndian, MacArm64Platform::Recognize);
293+ viewType->RegisterPlatformRecognizer (0 , LittleEndian, IOSArm64Platform::Recognize);
294+ viewType->RegisterPlatformRecognizer (0x0100000c , LittleEndian, IOSArm64Platform::Recognize);
173295 }
174296
175297 return true ;
0 commit comments