0

I recently compiled a C program I wrote with gcc on my x86 intel MacBook - I downloaded this binary onto my M1 MacBook and it seems to run fine... This challenges my understanding because I figured it had to be complied for a specific instruction set (x86 in this case).. I wonder if there is some software layer in my MacBook automatically 'assembling' the x86 into ARM

Any ideas?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

2 Answers2

3

MacOS contains Rosetta 2 software that does dynamic binary translation from x86, so that x86 software can be run on the M1 CPU. Not quite as efficient as code compiled directly from C to AArch64 machine code, but it works.

You can read more here: https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment

Stack Overflow has a tag for it: .

There's also a question on the Apple site: How does Rosetta 2 work? where answers point out that the translation is done once and cached, so it can spend significant time optimizing the translation. (For non-JITed x86 code.)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Klas-Kenny
  • 249
  • 1
  • 10
  • maybe you want to expand a little more on your answer so it doesn't appear as mostly links. In particular "you can read more here" + link is not a good sign. – Jean-François Fabre Nov 25 '21 at 09:24
  • @Jean-FrançoisFabre: To be fair, this question could basically be answered by retagging it, because SO has a tag for the relevant software, whose tag wiki links to wikipedia. :P There really isn't much more to say about the big picture than this: it's a *software* translation layer (although that key word was missing until my edit, so fair point). The other major interesting thing is that to support multi-threaded x86 code, M1 CPU cores can use a strongly-ordered memory model (x86 TSO) instead of the normal AArch64, so it doesn't have to figure out which stores need release/acquire sync. – Peter Cordes Nov 25 '21 at 09:32
  • https://www.infoq.com/news/2020/11/rosetta-2-translation/ / https://www.reddit.com/r/hardware/comments/i0mido/apple_silicon_has_a_runtime_toggle_for_tso_to/ / https://github.com/saagarjha/TSOEnabler - so it's software translation with hardware for memory-ordering. (Something qemu just ignores and hopes for the best on!) – Peter Cordes Nov 25 '21 at 09:35
1

Apple decided to transition from Intel to arm processors, which is a big decision due to the number of applications developed for the intel architecture over the years.

The Arm and the Intel instruction set are different, and programs compiled for Intel's architecture cannot natively run on the Arm's architecture. The instruction sets are protected, and it is illegal for a company to copy the instruction set of a competitor.

Rosetta is the solution to this instruction set incompatibility problem. Rosetta is an instruction translation that transforms Intel instructions into arm instructions. The performance impact can be negligible due to the performances of the M1 chip but the long term solution is to recompile the x86_64 application to the M1 architecture. XCode has already released the toolchain for this.

If you want to dig into the subject I recommend this article about the difference between the Intel and Arm architecture.

panic
  • 2,004
  • 2
  • 17
  • 33