7

I have a compilers course in college, as part of which we work on a nano pass compiler and create passes. So in the process there is a step

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

runtime.o is a compilation of runtime.c file.

and I get an error:

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
 ^

On further reading I found out it was because M1 Mac due to being ARM architecture does not directly compile x86_64 asm code. Is there any flag or any version of gcc that I can use to compile the x86 code on arm architecture?
I have seen rosetta and qemu, but I do not want to running a vm for such a task. qemu-static doesn't seem to work on M1 that straightforward.

The following are the contents of var_1_test.s (this file is generated by the compiler which only supports x86 [as per nature of course])

    .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

If any further details are needed I would be more than happy to provide. Thanks!

4
  • 3
    you need a cross compiler Commented Mar 10, 2022 at 9:57
  • 1
    You can't. periode Commented Mar 10, 2022 at 11:25
  • 2
    If MacOS builds their clang the way mainline clang is normally built, it will support multiple targets. e.g. clang -target x86-64 -c foo.s should assemble. IDK about linking into an executable, though; that would require a cross-linker and maybe libraries if you're not writing your own _start and using -nostdlib. On MacOS, if you haven't installed true GCC, the gcc command is actually clang, as you can see from gcc --version, but if you're using clang-specific options you should just invoke it as clang. Commented Mar 10, 2022 at 12:03
  • I get an illegal hardware instruction error. Any ideas on how to solve it?
    – Andrej
    Commented Mar 4 at 20:45

1 Answer 1

7

So after digging through a bunch more of Stack Overflow answer, and thanks to @paweł-Łukasik, through the term cross-compiler, I got an answer on how to run x86 code, or as a matter of fact any command in x86 architecture on cli using Rosetta 2.

How to run the Homebrew installer under Rosetta 2 on M1 Macbook

I made changes to makefile to add arch -x86_64 ...
and everything else ran perfectly.

Not the answer you're looking for? Browse other questions tagged or ask your own question.