NASM on Virtual Machine Ubuntu: Cannot execute binary file exec format error

14,163

The error:

error: Cannot execute binary file exec format error

Suggests your system can't understand the executable you are trying to run. In my comments I asked you to run uname -a so that I can find out what type of system you are running in your virtual machine. You gave the output as:

Linux dell 3.16.0-50-generic #67~14.04.1-Ubuntu SMP Fri...i686 i686 i686 GNU/LINUX

The i686 tells us this is a 32-bit version of Ubuntu, not 64-bit. Had the output included x86_64 then you would be on a 64-bit Ubuntu.

A 32-Bit OS can't directly run 64-bit applications. If you need to generate and run 64-bit code you will need to install a 64-bit Ubuntu OS.

A 64-bit Ubuntu system can be configured to allow development of 32 and 64-bit code by using multilib support. If building software with C/C++ (or just the C libraries) it might be useful to install these packages on Ubuntu:

sudo apt-get install gcc-multilib g++-multilib

Assuming you do install a 64-bit OS, the command you use to link your executable appears incorrect. You have:

nasm -f elf64 hello.asm -o hello.o    
ld -o hello.o hello -m elf_x86_64 
./hello

The NASM command looks okay. That assembles hello.asm to a 64-bit object file called hello.o . The LD command is being told to generate a 64-bit output file called hello.o from a file called hello. The commands should have looked like:

nasm -f elf64 hello.asm -o hello.o    
ld -o hello hello.o -m elf_x86_64 
./hello

Notice that we now use -o hello as we want to output an executable called hello from an object file called hello.o.

Share:
14,163
user5444075
Author by

user5444075

Updated on June 04, 2022

Comments

  • user5444075
    user5444075 almost 2 years

    I am getting an error after assembling a simple 64 bit hello world program. I am using the following commands:

    nasm -f elf64 hello.asm -o hello.o    successfull
    ld -o hello.o hello -m elf_x86_64     successfull
    ./hello
    

    error: Cannot execute binary file exec format error

    I am executing this in a 64 bit Ubuntu Virtual Machine. I appreciate your help!

  • user5444075
    user5444075 over 8 years
    Hi what do u mean by i may be having 32 bit one and yes i tried many binary formats while linking nothing seems to work. Also the program uses 64 bit registers. Thanks for helping
  • Top Sekret
    Top Sekret over 8 years
    @user5444075 you may just use gas or yasm. They seem to be better
  • user5444075
    user5444075 over 8 years
    I would but my professors support nasm, and teach everything on nasm plus im still a learner! I'd like to add that the same program is working on my friends pc. I think this may be happening because of virtual machine, maybe??
  • David C. Rankin
    David C. Rankin over 8 years
    and with the 64-bit version of the OS installed, he should be able to simply rely on the default target and use ld -o hello hello.o as the link command, dropping -m elf_x86_64 in its entirety.
  • Michael Petch
    Michael Petch over 8 years
    @DavidC.Rankin What you say is true, but leaving it in will still work, and it makes it clearer. You are correct the default would allow the -m elf_x86_64 to be dropped if the OP so chooses.