3

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!

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
user5444075
  • 57
  • 1
  • 3
  • 10
  • 1
    Are you really using `ld -o hello.o hello -m elf_x86_64`? Looks like you want `ld -o hello hello.o -m elf_x86_64` – Michael Petch Jan 08 '16 at 15:56
  • Are you absolutely sure you installed 64-bit Ubuntu? Maybe you installed 32-bit Ubuntu? What does `uname -a` show? – Michael Petch Jan 08 '16 at 16:03
  • Hey i just checked, both ways im getting the same error. – user5444075 Jan 08 '16 at 16:05
  • Linux dell 3.16.0-50-generic #67~14.04.1-Ubuntu SMP Fri...i686 i686 i686 GNU/LINUX uname -a – user5444075 Jan 08 '16 at 16:07
  • Please do the `uname -a` command and tell us what it outputs. As well before do the `ld` command do `rm hello` to make sure you don't have a junk file. Your version should not work. `ld -o hello.o hello` says to create an output file called `hello.o` from the file `hello`. that is not correct, and should throw an error unless you have a bogus file called `hello` laying around. – Michael Petch Jan 08 '16 at 16:08
  • I tried ur suggestion but still the same error!! For your knowledge i have installed ubuntu on a virtual machine.. – user5444075 Jan 08 '16 at 16:12
  • ok! so i can not use 64 bit operands on a 32 bit system?? – user5444075 Jan 08 '16 at 16:14
  • You can not run a 64-bit apps on a 32-bit OS. You can install a 64-bit OS in the virtual machine, and if properly configured it can run 64-bit and 32-bit apps. It probably works on your friends system because he installed a 64-bit OS (Ubuntu?) inside his virtual machine. On his system run `uname -a` and I bet `x86_64` appears somewhere in the output. – Michael Petch Jan 08 '16 at 16:15
  • So I need to install the 64 bit mode on VM!! Got it...Btw does the 32 bit version of the iso consists of option for 64 bit??? Thanks for your help!! – user5444075 Jan 08 '16 at 16:18
  • yes i used uname -a in the vm btw Thanks!! – user5444075 Jan 08 '16 at 16:20

2 Answers2

9

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • 1
    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. – David C. Rankin Jan 08 '16 at 17:06
  • @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. – Michael Petch Jan 08 '16 at 17:07
-1

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

Top Sekret
  • 748
  • 5
  • 21
  • 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 – user5444075 Jan 08 '16 at 15:24
  • @user5444075 you may just use gas or yasm. They seem to be better – Top Sekret Jan 08 '16 at 15:26
  • 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?? – user5444075 Jan 08 '16 at 15:33