如何在 M1 Mac (ARM) 上编译 x86_64 asm 代码

问题描述 投票:0回答:1

我在大学有一门编译器课程,作为该课程的一部分,我们研究 Nano Pass 编译器并创建 Pass。所以这个过程中有一个步骤

gcc -g -std=c99 runtime.o tests/var_test_1.s

runtime.o 是 runtime.c 文件的编译。

我收到一个错误:

tests/var_test_1.s:3:12: error: unknown token in expression
 movq $42, %rax
           ^
tests/var_test_1.s:3:12: error: invalid operand
 movq $42, %rax
           ^
tests/var_test_1.s:4:2: error: unrecognized instruction mnemonic, did you mean: cmp?
 jmp _conclusion
 ^
tests/var_test_1.s:9:8: error: unknown token in expression
 pushq %rbp
       ^
tests/var_test_1.s:9:8: error: invalid operand
 pushq %rbp
       ^
tests/var_test_1.s:10:7: error: unknown token in expression
 movq %rsp, %rbp
      ^
tests/var_test_1.s:10:7: error: invalid operand
 movq %rsp, %rbp
      ^
tests/var_test_1.s:11:11: error: unknown token in expression
 subq $0, %rsp
          ^
tests/var_test_1.s:11:11: error: invalid operand
 subq $0, %rsp
          ^
tests/var_test_1.s:12:2: error: unrecognized instruction mnemonic, did you mean: cmp?
 jmp _start
 ^
tests/var_test_1.s:16:11: error: unknown token in expression
 addq $0, %rsp
          ^
tests/var_test_1.s:16:11: error: invalid operand
 addq $0, %rsp
          ^
tests/var_test_1.s:17:7: error: unknown token in expression
 popq %rbp
      ^
tests/var_test_1.s:17:7: error: invalid operand
 popq %rbp
      ^
tests/var_test_1.s:18:2: error: unrecognized instruction mnemonic, did you mean: eret, ret?
 retq
 ^

进一步阅读我发现这是因为M1 Mac由于是ARM架构,不能直接编译x86_64 asm代码。 是否有任何标志或任何版本的gcc可以用来在arm架构上编译x86代码?
我见过 Rosetta 和 qemu,但我不想为这样的任务运行虚拟机。 qemu-static 似乎在 M1 上工作不那么简单。

以下是var_1_test.s的内容(该文件是由仅支持x86的编译器生成的[当然是根据性质])

    .align 16
_start:
    movq    $42, %rax
    jmp _conclusion

    .globl _main
    .align 16
_main:
    pushq   %rbp
    movq    %rsp, %rbp
    subq    $0, %rsp
    jmp _start

    .align 16
_conclusion:
    addq    $0, %rsp
    popq    %rbp
    ret

如果需要任何进一步的细节,我将非常乐意提供。 谢谢!

macos assembly x86-64 cross-compiling apple-m1
1个回答
5
投票

因此,在挖掘了更多 Stack Overflow 答案后,并感谢 @paweł-Łukasik,通过术语交叉编译器,我得到了关于如何运行 x86 代码的答案,或者事实上任何命令使用 Rosetta 2 的 cli 上的 x86 架构。

如何在 M1 Macbook 上的 Rosetta 2 下运行 Homebrew 安装程序

我对 makefile 进行了更改以添加

arch -x86_64 ...

其他一切都运行得很完美。

© www.soinside.com 2019 - 2024. All rights reserved.