Home > Software engineering >  Why can I run x86 binary on ARM?
Why can I run x86 binary on ARM?

Time:11-25

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?

CodePudding user response:

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.)

CodePudding user response:

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.

  • Related