Ubuntu – Unable to run a 32-bit program on 64-bit VM


I compiled a small "hello world" C program on Ubuntu 10.04 32 bit machine. Then I put the executable(a.out) on Ubuntu 12.10(64 bit) VMWare virtual machine and tried to execute it(./a.out). The OS does not identify the file and says 'No such file or directory'. But when I put the same executable on a Ubuntu 12.10(64 bit) running on a 64 bit laptop, it runs fine and I get the desired output.
The VM is running on IBM Blade Servers. I wanted to know why the results for a VM?

Best Answer

The message No such file or directory does not refer to your executable file: a.out. Instead it refers to a helper program that’s needed to run the 32 bit dynamically linked executable a.out.

Now, I found all of these very well explained in this nice article:

Static and dynamic linkage

There are two types of binary executables: statically linked and dynamically linked ones. First about the statically linked ones: When a program wants to call a library function, it refers to it by name. When building the program from source, all library functions used in the program are copied from the library into the program. The program then contains its own code as well as the code of the library functions it uses. Then in the calling places the name is changed to the address of the corresponding function in the program. This process is called linking because it links together the name of a function with the function itself, its implementation. It’s called static, because the link cannot be changed after the program has been built.

Dynamically linked programs work differently: The program also refers to library functions by name. When building the program, two lists are assembled and stored together with the program: a list of which library functions are used in which places, and a list of the libraries that contain the functions used by the program. That’s all for building the program.

Later, at execution time, a special helper program, the so-called dynamic linker, looks in specific places in the file system for each library on the library list and loads it into memory. Now the dynamic linker knows at what memory addresses the library functions are available. It uses the first list to write the correct address in all places that call library functions. Then the dynamically linked program can be run.

Related Question