2

NASM compiles just fine, but when i use YASM I'm getting the following error:

hello.asm:12: error: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode, consider "[_symbol wrt rip]" for mem access, "qword" and "dq _foo" for pointers.

Makefile

test: hello
    ./hello
hello:
    yasm -f macho64 hello.asm   
    ld -o hello hello.o
clean:
    rm *.o *.core hello

system.inc

%define stdin       0
%define stdout      1
%define stderr      2

%define SYS_nosys   0
%define SYS_exit    1
%define SYS_fork    2
%define SYS_read    3
%define SYS_write   4

section .text
align 4
access.the.osx.kernel:
    syscall
    ret

%macro  system  1
    mov rax, %1
    call    access.the.osx.kernel
%endmacro

%macro  sys.exit    0
    system  SYS_exit
%endmacro

%macro  sys.write   0
    system  SYS_write
%endmacro

hello.asm

%include 'system.inc'

section .data
hello   db  'Hello, World!', 0Ah
hbytes  equ $-hello

section .text
global  start
start:
mov rax, 0x2000004
mov rdi, stdout
mov rsi, hello
mov rdx, hbytes
syscall
;sys.write

xor rdi, rdi
mov rax, 0x2000001
syscall
;sys.exit

Anyone know what's going on? And if you could explain why NASM works, but YASM doesn't that would be a bonus.

EhevuTov
  • 20,205
  • 16
  • 66
  • 71
  • I think you need `lea rsi, hello` or `mov rsi, offset hello` or something in that vein. – Igor Skochinsky Jan 15 '13 at 17:27
  • @IgorSkochinsky I think you're right. I'm just not sure what it is. I'm reading the YASM manual to see what I need to do. I think NASM is ok with deducing, by with YASM I think you need to be explicit on size(I think). – EhevuTov Jan 15 '13 at 17:30
  • Using `default rel` may fix this. More info here: http://www.tortall.net/projects/yasm/manual/html/nasm-effaddr.html – harold Jan 15 '13 at 17:30
  • @harold thanks, that's actually what I was reading. – EhevuTov Jan 15 '13 at 17:34

1 Answers1

3

I got it working. In yasm you have to explicitly tell it that the address is 64-bit like so:

mov rsi, qword hello

The documentation talks about the situation here: https://github.com/yasm/yasm/wiki/AMD64

EhevuTov
  • 20,205
  • 16
  • 66
  • 71
  • A RIP-relative LEA is smaller code-size and probably a better choice. (It's what compilers use, and makes your code actually position-independent, which I think Mach-O requires anyway.) **Use a `default rel` directive, or an explicit REL in `lea rsi, [rel hello]`**. – Peter Cordes Dec 08 '17 at 06:01