OpenBSD – Hello World in x64 Assembly

This past week I’ve been learning x86 assembly language on an old laptop, purely because I couldn’t get anything running on my x64 OpenBSD machine. My 64 bit helloworld.s was as follows:

section .data

 msg db "Hello world!", 10
 len equ $-msg

;syscalls
 %define SYS_exit 1
 %define SYS_write 4

section .text

global _start
_start:

 mov rax, SYS_write
 mov rdi, 1 ;stdout
 mov rsi, msg
 mov rdx, len

 syscall

 mov rax, SYS_exit
 xor rdi, rdi

 syscall

Giving the errors:

jon@OpenBsD:~/dev/asm;$ nasm -f elf64 -o helloworld.o helloworld.s
jon@OpenBsD:~/dev/asm;$ ld -o helloworld helloworld.o
ld: warning: creating a DT_TEXTREL in a shared object.
jon@OpenBsD:~/dev/asm;$ yasm -f elf64 -o helloworld.o helloworld.s
jon@OpenBsD:~/dev/asm;$ ld -o helloworld helloworld.o 
ld: helloworld.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
helloworld.o: could not read symbols: Bad value

I did search /usr/src for any assembly files I could straight up copy and test, but I didn’t find anything suitable, however the code looks fine and as the assembler didn’t throw out any errors I’m thinking this to be an issue with the linker.

Running ld with the verbose flag we are shown the options for available architectures:

jon@OpenBsD:~>$ ld -V
GNU ld version 2.17
  Supported emulations:
   elf_x86_64_obsd
   elf_i386_obsd
   elf_i386

Continue reading “OpenBSD – Hello World in x64 Assembly”