3

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 ?

Michael Spector
  • 36,723
  • 6
  • 60
  • 88
user2037008
  • 31
  • 1
  • 3
  • related: [building static / dynamic binaries from asm with GNU tools](https://stackoverflow.com/questions/36861903/assembling-32-bit-binaries-on-a-64-bit-system-gnu-toolchain). `_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 Sep 30 '17 at 04:38

2 Answers2

3

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
Michael Spector
  • 36,723
  • 6
  • 60
  • 88
0

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

Styx
  • 9,863
  • 8
  • 43
  • 53