Home>
Assembly language link error on macOS

An error occurred when trying to link the source code in Shosuisha's "Low Level Programming" on the mac terminal. I tried writing and setting the environment while looking at the Github page of this book, but I couldn't solve it anyway, so I would like to lend my wisdom.

Github for Low Level Programming: https://github.com/Apress/low-level-programming

Error message
% nasm -fmacho64 hello.asm&&ld hello.o&&./a.out
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol (s) not found for architecture x86_64
Applicable source code
global start
          section .text
start: mov rax, 0x02000004
          mov rdi, 1
          mov rsi, message
          mov rdx, 13
          syscall
          mov rax, 0x02000001
          xor rdi, rdi
          syscall
          section .data
message: db "Hello, World", 10

I changed what was originally _start to start, or changed it to main or _main.

Supplemental information (FW/tool version etc.)

mid-2012 13inch macbook air
intel core i5
macOS Catalina version 10.15.1
NASM version 2.14.02 compiled on Sep 28 2019

Append otn

2019/11/03 00:50

  I saw it as

_main.
  There are two places in start, have you both _main?

If both are _main

ld: dynamic main executables must link with libSystem.dylib for architecture x86_64
Another error message was

. Link and run

% nasm -fmacho64 hello.asm&&ld hello.o&&./a.out
It remains

. Thanks for your advice.

asm-san

2019/11/03 01:20

  

On github
  >Linking: ld -macosx_version_min 10.7.0 -lSystem -o hello_world
  Did you try this option?

If i try to link with that option

ld: warning: -arch not specified
ld: warning: ignoring file /usr/lib/libSystem.dylib, missing required architecture unknown in file /usr/lib/libSystem.dylib (2 slices)
Undefined symbols for architecture unknown:
  "start", referenced from:
     implicit entry/start for main executable
ld: symbol (s) not found for architecture unknown


The message has changed. By the way, the two parts that were the contents ofstartwere changed to_mainin response to otn's indication. Thanks for your advice.

  • Answer # 1

    I think the github article about command line option specification was old at the time of writing (two years ago), and the target macOS version was also old. I tried it with macOS Mojave, but it was assembled by replacing the "start" with "_main" in the two places that were pointed out in the comments, and removing the "-macosx_version_min" option of theldcommand I was able to link with. Questioner's macOS is Catalina, but for reference.

    bash-3.2 $cat hello2.asm
              global _main
              section .text
    _main: mov rax, 0x02000004
              mov rdi, 1
              mov rsi, message
              mov rdx, 13
              syscall
              mov rax, 0x02000001
              xor rdi, rdi
              syscall
              section .data
    message: db "Hello, World", 10
    bash-3.2 $nasm -fmacho64 hello2.asm
    bash-3.2 $ld hello2.o -lSystem -o hello2
    bash-3.2 $./hello2
    Hello, World
    bash-3.2 $

  • Answer # 2

    Are you aware of the cause (s) of Undefined symbols for architecture x86_64 :?