Stan
Stan

Reputation: 329

How to cross compile arm64 assembly from linux to mac

I'm trying to cross compile assembly hello world

Currently when I run the linux build it says "exec format error: ./linuxbuild.out" with error code 126 and some differences in objdump shown below. I tried using -F on the linker and lazy lib without success. One difference in the dump is __mh_execute_header is missing

Here's what I did

On x86-64 linux I have the assembly

$ cat hello.s 
    .global _main
    .align 2

    _main: mov X0, #1
    adr     X1, hello
    mov     X2, #13
    mov     X16, #4
    svc     0

    mov     X0, #0
    mov     X16, #1
    svc     0

    hello: .ascii  "Hello\n"

I wrote

clang hello.s --target=arm64-apple-macosx12.5.0 -c -o h.o
ld64.lld h.o -arch arm64 -platform_version macos 12 5 -v -dylib -o linuxbuild.out

The output

LLD 14.0.6
Library search paths:
    /usr/lib
    /usr/local/lib
Framework search paths:
ld64.lld: warning: h.o has version 12.5.0, which is newer than target minimum of 12

You can see the difference

% /opt/homebrew/opt/binutils/bin/objdump -x a.out linuxbuild.out

a.out:     file format mach-o-arm64
a.out
architecture: aarch64, flags 0x00000012:
EXEC_P, HAS_SYMS
start address 0x0000000100003f90
MACH-O header:
  magic:      0xfeedfacf
  cputype:    0x100000c (ARM64)
  cpusubtype: 0 (ARM64_ALL)
  filetype:   0x2
  ncmds:      0x10
  sizeocmds:  0x2e8
  flags:      0x200085
  version:    2

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000026  0000000100003f90  0000000100003f90  00003f90  2**2
                  CONTENTS, ALLOC, LOAD, CODE
  1 __TEXT.__unwind_info 00000048  0000000100003fb8  0000000100003fb8  00003fb8  2**2
                  CONTENTS, ALLOC, LOAD, READONLY, CODE
SYMBOL TABLE:
0000000100003fb0 l       0e SECT   01 0000 [.text] hello
0000000100000000 g       0f SECT   01 0010 [.text] __mh_execute_header
0000000100003f90 g       0f SECT   01 0000 [.text] _main



linuxbuild.out:     file format mach-o-arm64
linuxbuild.out
architecture: aarch64, flags 0x00000050:
HAS_SYMS, DYNAMIC
start address 0x0000000000000000
MACH-O header:
  magic:      0xfeedfacf
  cputype:    0x100000c (ARM64)
  cpusubtype: 0 (ARM64_ALL)
  filetype:   0x6
  ncmds:      0xb
  sizeocmds:  0x208
  flags:      0x100085
  version:    2

Sections:
Idx Name          Size      VMA               LMA               File off  Algn
  0 .text         00000026  0000000000000248  0000000000000248  00000248  2**2
                  CONTENTS, ALLOC, LOAD, CODE
SYMBOL TABLE:
0000000000000268 l       0e SECT   01 0000 [.text] hello
0000000000000248 g       0f SECT   01 0000 [.text] _main

Upvotes: 1

Views: 636

Answers (1)

Stan
Stan

Reputation: 329

I figured it out by writing clang -v on my mac, using the linker then repeating it on linux. I have 0 mac files on linux. It seems to work on my simple assembly file

ld64.lld -dynamic -arch arm64 -platform_version macos 12.0.0 13.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk h.o -o linux-to-mac

Upvotes: 2

Related Questions