简体   繁体   English

在OSX上链接到iOS模拟器二进制文件

[英]Linking to iOS simulator binaries on OSX

I was curious what would happen if I linked to an iOS simulator framework in a Mac app. 我很好奇如果我在Mac应用程序中链接到iOS模拟器框架会发生什么。 So I copied UIKit to it's own folder (so the Framework search path wouldn't include all the iOS simulator framework, as like CoreFoundation is both on Mac and iOS but has different headers), and dragged it into the link section in Xcode. 所以我将UIKit复制到它自己的文件夹(因此框架搜索路径不会包含所有iOS模拟器框架,就像CoreFoundation在Mac和iOS上都有不同的标题),并将其拖入Xcode的链接部分。 The error Xcode gave me was: Xcode给我的错误是:

building for MacOSX, but linking against dylib built for iOS Simulator file '/Users/jonathan/Desktop/macuikit/UIKit.framework/UIKit' for architecture x86_64 为MacOSX构建,但是针对架构x86_64为iOS模拟器文件'/Users/jonathan/Desktop/macuikit/UIKit.framework/UIKit'构建的dylib链接

Both architectures are x86_64, so how can it tell the framework is specifically for the iOS simulator, I removed all references to iOS in things like Info.plist, even tried deleted everything but the UIKit binary, but the same error came up. 这两种架构都是x86_64,所以它怎么能告诉框架是专门针对iOS模拟器的,我删除了对Info.plist之类的所有iOS的引用,甚至尝试删除除了UIKit二进制文件之外的一切,但同样的错误出现了。 Is there something in the binary it self that tells the linker which platform it can run on, rather than just the architecture? 它自己的二进制文件中有什么东西可以告诉链接器它可以运行哪个平台,而不仅仅是架构? I looked at the Mach-O header but there is only fields for CPU type and subtype, and neither has a value for the simulator as expected. 我查看了Mach-O标头但是只有CPU类型和子类型的字段,并且没有按预期的模拟器值。

After a little bit of digging, it turns out that the platform on which the library can run on is indeed specified in the binary. 经过一些挖掘后,事实证明可以运行库的平台确实在二进制文件中指定。 In fact, you can edit the binary in your favorite Hex editor and make the linker skip this check entirely. 实际上,您可以在您喜欢的Hex编辑器中编辑二进制文件,并使链接器完全跳过此检查。

This information is not specified in the Mach-O header (as you have already realized). 此信息未在Mach-O标头中指定(正如您已经意识到的那样)。 Instead, it is specified as a load command type. 相反,它被指定为加载命令类型。 You can see the available types by digging through the LLVM sources . 您可以通过挖掘LLVM源来查看可用的类型。 Specifically, the enum values LC_VERSION_MIN_MACOSX and LC_VERSION_MIN_IPHONEOS look interesting. 具体来说,枚举值LC_VERSION_MIN_MACOSXLC_VERSION_MIN_IPHONEOS看起来很有趣。

Now, find the offset for this in our binary. 现在,在我们的二进制文件中找到它的偏移量。 Open the same in MachOView (or any other editor/viewer or your choice) and note the offset: MachOView (或任何其他编辑器/查看器或您选择的)中打开相同的MachOView并记下偏移量:

寻找抵消

Once the offset is noted, jump to the same in a Hex editor and update it. 注意到偏移量后,在Hex编辑器中跳转到相同的位置并更新它。 I modified LC_VERSION_MIN_IPHONEOS (25) to LC_VERSION_MIN_MACOSX (24) 我将LC_VERSION_MIN_IPHONEOS (25)修改为LC_VERSION_MIN_MACOSX (24)

Save the updates and try linking again. 保存更新并再次尝试链接。 The error should go away. 错误应该消失。 Of course, you will hit other issues when you try to actually run your example. 当然,当您尝试实际运行示例时,您将遇到其他问题。 Have fun with LLDB then :) 享受LLDB乐趣然后:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM