user5444075
user5444075

Reputation: 57

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

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!

Upvotes: 3

Views: 16799

Answers (2)

Michael Petch
Michael Petch

Reputation: 47653

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.

Upvotes: 10

Top Sekret
Top Sekret

Reputation: 758

You may have 32-bit one, check once again. Also, as help says, there are more binary formats, try following: elfx32, elf32, elf.

Upvotes: -1

Related Questions