Reputation: 4349
I have the following assembly code (copied from here):
hello.asm
:
section .data
msg: db 'hello, world!', 0
section .text
global _start
_start:
mov rax, 4
mov rdi, 1
mov rsi, qword msg ; I added qword because the compiler complained
mov rdx, 13
syscall
mov rax, 1
xor rdi, rdi
syscall
As explained in the comment above I added qword
because I recieved the following error message:
$ yasm -f macho64 hello.asm
hello.asm:10: 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.
After making that change, both compiling and linking succeed, but I get a bus error while running:
$ yasm -f macho64 hello.asm
$ ld -o hello -e _start hello.o
$ ./hello
bus error ./hello
Is the addition of qword
incorrect, or is there something else I need to change?
Upvotes: 1
Views: 986
Reputation: 58427
64-bit system calls on OSX need to have a class specifier in bits 24..31 of the register that holds the system call number (rax
in your case). You've implicitly used the class specifier 0, which is invalid.
You probably want the UNIX/BSD class, which means that you should be adding 2<<24
(0x2000000
). So these lines:
mov rax, 4
mov rax, 1
should be changed to:
mov rax, 0x2000004
mov rax, 0x2000001
Also, I don't see what purpose that sub rsp, 8
at the end is serving, so you can probably remove that line.
Upvotes: 4