-2

I am currently studying X86_Intel 64-bit assembly and I want to run my assembly code on macOS. I have my code compiling and linking correctly on an Ubuntu VM using:

 yasm -f elf64 -g dwarf2 -l div.lst div.asm 
 ld -o div div.o

However, when I use the same commands above I run into issues on macOS. Is there anything blatantly obvious that I am missing?

Jonathans-MacBook-Pro:Practical 2 jonathancopeland$ yasm --version
yasm 1.3.0
Compiled on Sep 15 2017.Copyright (c) 2001-2014 Peter Johnson and other Yasm developers.
Run yasm --license for licensing overview and summary.
Jonathans-MacBook-Pro:Practical 2 jonathancopeland$ yasm -f elf64 -g dwarf2 -l div.lst div.asm
Jonathans-MacBook-Pro:Practical 2 jonathancopeland$ ld -o div div.o
**ld: warning: -arch not specified
ld: warning: -macosx_version_min not specified, assuming 10.11ld: warning: ignoring file div.o, file was built for unsupportedfile format ( 0x7F 0x45 0x4C 0x46 0x02 0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): div.o
Undefined symbols for architecture x86_64:
  "start", referenced from:
 implicit entry/start for main executable
ld: symbol(s) not found for inferred architecture x86_64
Jonathans-MacBook-Pro:Practical 2 jonathancopeland$**

Here is a screenshot of my code, file structure and terminal

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
  • Function names are decorated with a leading underscore on macOS. Also, do not post pictures of code. Always post code as text! Please update your post to paste all your code into the question. I'm going to retract my downvote once you do. – fuz Aug 17 '18 at 10:39
  • OS X uses `macho64`, not `elf64`. – Peter Cordes Aug 17 '18 at 10:51
  • so you run Mac OS X inside VM? (then the information about Ubuntu is not truly relevant, only would you encounter some weird bug in VM itself). Do you run Ubuntu inside VM on Mac OS? (then the information about Mac is not relevant, because your code inside VM will run on linux, not on mac). It's not clear to me by quick read of question, what is what and what are you trying to achieve. – Ped7g Aug 17 '18 at 12:07

1 Answers1

1

First of all, assembly code is generally not portable between platforms, even on the same CPU architecture. If you know exactly what you are doing, you can get your assembly code to be portable, but it requires good knowledge of the system.

There are three main issues with your code:

  1. macOS is not an ELF target. You instruct yasm to generate an ELF object which the macOS link editor does not understand. You need to generate a Mach-O object to succesfully create a binary. Refer to the documentation of your assembler for how to do that.
  2. macOS has a different default name for the entry point, I think it's just start. Refer to the documentation of the link editor for details.
  3. System calls have different numbers and some times different arguments. See this list for details. Using Linux system calls generally won't work. For a portable solution, use the system call wrappers from the libc, but keep in mind that symbols on macOS are decorated with a leading underscore. For example, the symbol for the C function write is named _write.
fuz
  • 76,641
  • 24
  • 165
  • 316
  • [nasm/gcc issue on 64-bit Mac OS X Lion](https://stackoverflow.com/a/9310051) for elf vs. macho and leading underscores. But yes, not a duplicate because of point 3: Linux and OS X aren't asm-compatible. – Peter Cordes Aug 17 '18 at 10:56
  • 1
    @PeterCordes I'm pretty sure I've seen all individual bits of OPs problem somewhere before, but I'm not sure if I've seen it in this exact combination. – fuz Aug 17 '18 at 11:14