3
votes

I am trying to write a simple hello world program in 64-bit assembly and run on Ubuntu 64 bit. The program is as follows :

global _start           ; entry point export for ld section .text   
_start:     ; system call to write message to stdout
    mov rax, 1      ; sys_write
    mov rdi, 1      ; stdout
    mov rsi, mes    ; message address
    mov rdx, len    ; message length
    syscall     ; exit sys call
    mov rax, 60     ; exit call id
    mov rdi, 0      ; return success
    syscall
section .data
    mes: db 'Hello, world!',0x0A    ; message
    len :   equ $-mes   

I assembled it using nasm -f elf64 hello64.asm and tried linking it using ld -o hello64 hello64.o it gives me following error -

ld: i386:x86-64 architecture of input file `hello64.o' is incompatible with i386 output

I get same error even when using flags --oformat elf64-x86-64 or elf64-little or elf64-big.

can someone help out ?

2
related: building static / dynamic binaries from asm with GNU tools. _start or main, with/without libc. Possibly you're using a 32-bit install of Ubuntu? Try file /usr/bin/ld, although I'd actually expect a 32-bit ld to be able to make 64-bit executables with the right --oformat. - Peter Cordes

2 Answers

3
votes

The following works on my system:

nasm -f elf64 hello64.asm
ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello64 hello64.o
0
votes

You might consider running apt-get update again. I'm running it on an updated version 15.04 and it's working for me.