0

I wrote a very simple C program and my goal is to build an x86-only binary and then run that binary on an Apple Silicon Mac and have Rosetta 2 translate it into a Universal 2 binary. I want to look at the Rosetta 2-translated binary specifically; I'm not looking to build a Universal 2 app myself (that would be trivial).

I attempted to run the program through Terminal ($ ./helloworld) and even stick it inside of a .app, put it in the Applications folder, and double-click it. In both cases, the program runs correctly, but according to $ file helloworld, the binary is still x86-only (Rosetta 2 didn't add an arm64 executable).

Any idea how to force Rosetta 2 to translate a binary?

7
  • Are you looking for Rosetta to create an arm64 executable from the x86 binary? Why not just build the app as a arm64 to begin with?
    – ErniePC12
    Nov 18, 2020 at 22:41
  • 1
    @ErniePC12 Yes. I want to disassemble the translated arm64 binary and compare it to a natively-built binary.
    – nrubin29
    Nov 18, 2020 at 22:49
  • To force the app to open under Rosetta, check the box in the Get Info window. But I'm not sure that it would modify the app with the translated binary. It would probably place that translated binary somewhere in a sandbox. Maybe check /Library/Application Support/?
    – ErniePC12
    Nov 19, 2020 at 0:24
  • 1
    Wowfunhappy: completely wrong. Rosetta2 is a translator.
    – gnasher729
    Nov 19, 2020 at 1:01
  • 1
    @Wowfunhappy I'm aware that some binaries can't be fully translated ahead-of-time (e.g. JIT code), but Rosetta 2 does do some translation at install-time. In the case of my hello world program, it should be able to translate everything, but even if not, it must translate something. My goal is to figure out how to trigger this translation and where the translated binary ends up.
    – nrubin29
    Nov 19, 2020 at 8:10

2 Answers 2

2

This is a bit late, but you can directly run oahd-helper and pass it two file descriptors (the x86 binary to translate as the first and a file to output the translated bin to as the second). The location of oahd-helper varies (on Big Sur it’s in /Library/Apple/usr/libexec/oah, on Monterey its /usr/libexec/rosetta). I made a gist that makes the process simpler, you can find it here: https://gist.github.com/sunflsks/00fe7c740f3b1d9668f55dff80707d03

4
  • 1
    Thank you! Your bash script worked for me.
    – nrubin29
    Nov 11, 2021 at 2:17
  • Is this capable of working on individual object files, or full binaries only? Is there anything capable of doing it at an object file level? Mar 25 at 21:09
  • @MalcolmMacLeod did you ever discover if this could convert objects?
    – David
    May 23 at 3:24
  • 1
    @David Unfortunately not, temporarily put the task I had in mind for this on hold for more important things... Still something I need to look at in the near future so very interested in the answer. Will update here when/if I try if nobody else gets there first. May 23 at 19:49
2

If you've written a program in C, simply compile the program twice, using Mac's version of gcc, and then use lipo to combine them into one program.

See https://developer.apple.com/documentation/xcode/building_a_universal_macos_binary for Apple's explanation of the process to create a universal Binary (containing both x86_64 and ARM64)

1
  • Thanks for the answer. My goal is to disassemble the Rosetta 2-translated binary and compare it to a natively-compiled binary (to get an idea of how Rosetta 2 works). I updated my question to make this clear. Also, to add on to your answer, if the goal is to make a Universal 2 binary (which it isn't in my case), you can just use Xcode and set the build device to "Any Mac (Apple Silicon, Intel)".
    – nrubin29
    Nov 19, 2020 at 8:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .