what does `arch -x86_64 zsh` on m1 mac do internally?
Asked Answered
Y

1

7

I encountered this command while trying to find a workaround for installing old version of npm packages that came out before m1 mac (darwin arm64)

I understand that this will try to launch a zsh session with x86_64 architecture. (although running arch again will give me i386 which is somehow compatible with x86_64 I think.)

But as far as my knowledge goes, architecture is defined in hardware as processor chips. And to emulate a different architecture you need some heavier software like virtualbox or qemu.

So what does the arch -x86_64 zsh actually do? Does it actually emulate a different architecture?

I tried reading the man arch stuff, but too much stuff to actually understand for me.

...
DESCRIPTION
     The arch command with no arguments, displays the machine's architecture type.

     The other use of the arch command is to run a selected architecture of a universal binary.  A universal
     binary contains code that can run on different architectures.  By default, the operating system will select
     the architecture that most closely matches the processor type.  A 64-bit architecture is preferred over a
     32-bit architecture on a 64-bit processor, while only 32-bit architectures can run on a 32-bit processor.

     When the most natural architecture is unavailable, the operating system will try to pick another
     architecture.  On 64-bit processors, a 32-bit architecture is tried.  Otherwise, no architecture is run, and
     an error results.

     The arch command can be used to alter the operating system's normal selection order.  The most common use is
     to select the 32-bit architecture on a 64-bit processor, even if a 64-bit architecture is available.

     The arch_name argument must be one of the currently supported architectures:

           i386     32-bit intel

           x86_64   64-bit intel

           x86_64h  64-bit intel (haswell)

           arm64    64-bit arm

           arm64e   64-bit arm (Apple Silicon)
...
Yonkers answered 25/1, 2023 at 5:27 Comment(0)
T
4

A universal binary contains multiple separate binary files. On execution, the most compatible binary is selected to actually be executed.

The arch command simply overrides this automatic selection by explicitly choosing which to run. Whether you can run the selected executable file on the current platform is another matter.

If you think of a universal binary as a hash table, then arch is just the lookup operation.

Traci answered 27/1, 2023 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.