-1

My current setup is a m1 MacBook Air.

I am reading a low level programming book.
I want the the C code that I write to compile to x86_64 assembly.

With clang I can do that pretty easily:

clang -target x86_64 -masm=intel -S add_two_numbers.c

But it does not work when I include a library (eg. stdio).

❯ clang -target x86_64 -masm=intel -S hello.c
hello.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~

As clang docs says I can manually install x86_64 libraries and do:

clang -target x86_64 -masm=intel -I path/to/Include -L path/to/Library -S hello.c

But I can't find prebuilt packages to download on MacOS. I tried cross compilation, it takes too much effort.

So I ditched and went for something simpler. I found a solution to this problem that I shall share as an answer below.

  • Check Clang cross compilation manual https://clang.llvm.org/docs/CrossCompilation.html You need to specify a toolchain executable, to link with libc fir target CPU. – Victor Gubin Mar 24 '23 at 16:12
  • P.S. one more option, use online compiler explorer https://godbolt.org Simply select architecture and compiller you need in output, and take the result. – Victor Gubin Mar 24 '23 at 16:13
  • @VictorGubin Thanks for the quick reply. The online explorer is good. Can you suggest - How can I debug x86_64 asm on apple silicon?. Like seeing memory, registers, etc. I tried qemu, it did not work. – Tirtharaj Pramanik Mar 24 '23 at 16:35
  • @Tirtharaj Pramanik use some virtual machine i.e. VirtualBox. ARM architecture can not run Intel binary code directly, as well as debug (running with dubbing app) – Victor Gubin Mar 24 '23 at 22:11

2 Answers2

3

Use -arch, not -target.

Example source / command line / output:

#include <stdio.h>

int main(void)
{
    printf("Hello world\n");
    return 0;
}
clang -arch x86_64 -masm=intel -S -Wall -O3 -o - t.c 
    .section    __TEXT,__text,regular,pure_instructions
    .build_version macos, 13, 0 sdk_version 13, 1
    .intel_syntax noprefix
    .globl  _main                           ## -- Begin function main
    .p2align    4, 0x90
_main:                                  ## @main
    .cfi_startproc
## %bb.0:
    push    rbp
    .cfi_def_cfa_offset 16
    .cfi_offset rbp, -16
    mov rbp, rsp
    .cfi_def_cfa_register rbp
    lea rdi, [rip + L_str]
    call    _puts
    xor eax, eax
    pop rbp
    ret
    .cfi_endproc
                                        ## -- End function
    .section    __TEXT,__cstring,cstring_literals
L_str:                                  ## @str
    .asciz  "Hello world"

.subsections_via_symbols

Alternatively, if you want to use -target, you need to specify a target triplet like x86_64-apple-macos13:

clang -target x86_64-apple-macos13 -masm=intel -S -Wall -O3 -o - t.c

If you only specify x86_64, that is treated as x86_64-unknown-unknown.

Siguza
  • 21,155
  • 6
  • 52
  • 89
-1
[Solution]

❯ sudo port install x86_64-elf-gcc

❯ x86_64-elf-gcc -masm=intel -S hello.c

Hello Hackers  (for decoration purposes only.)

TLDR; I can't execute this code on my arm processor. so no hello world here!!

And it works!