Usage differences between. a.out, .ELF, .EXE, and .COFF

ha9u63ar picture ha9u63ar · Jun 27, 2013 · Viewed 13.8k times · Source

Don't get me wrong by looking at the question title - I know what they are (format for portable executable files). But my interest scope is slightly different

MY CONFUSION

I am involved in re-hosting/retargeting applications that are originally from third parties. The problem is that sometimes the formats for object codes are also in .elf, .COFF formats and still says, "Executable and linkable".

I am primarily a Windows user and know that when you compile and assemble your C/C++ code, you get something similar to .o or .obj. that are not executable (well, I never tried to execute them). But when you complete linking static and dynamic libraries and finish building, the executable appears. My understanding is that you can then go about and link that executable or "bash" test it with some form of script if necessary.

However, in Linux (or UNIX-like systems) there are .o files after you compile and assemble the C/C++ code. And once the linking is done, the executable is in a.out format (at least in Ubuntu distribution of Linux). It may very well be .elf in some other distrib. In my quick web search none of the sources mentioned anything about .o files as executables.

QUESTIONS

Therefore my question turns into the followings:

  1. What is the true definitions for portable executables and object code?

  2. How is it that Windows and UNIX platform covers both executables annd object code under the same file format (.COFF, .elf).

  3. Am I misinterpreting "Linkable"? My interpretation of "Linkable" is something that is compiled object code and can then be "linked" to other static/dynamic link libraries. Is this a stupid thought?

  4. Based on question 1. (and perhaps 2) do I need to use symbol tables (e.g. .LUM or .MAP files) with object code then? Symbols as in debug symbols and using them when re-hosting the executables/object files on a different machine.

Thanks in advance for the right nudges. Meanwhile, I will keep digging and update the question if necessary.

UPDATE

I have managed to dig this out from somewhere :( Seems like a lot to swallow to me.

Answer

user529758 picture user529758 · Jun 27, 2013

I am primarily a Windows user and know that when you compile your C/C++ code, you get something similar to .o or .obj. that are not executable

Well, last time I compiled stuff on Windows, the result of the compilation was an .obj file, which is exactly what its name suggests: it's an object file. You're right in that it's not an executable in itself. It contains machine code which doesn't (yet) contain enough information to be directly run on the CPU.

However, in Linux (or UNIX-like systems) there are .o files after you compile the C/C++ code. And once the linking is done, the executable is in a.out format (at least in Ubuntu distribution of Linux). It may very well be .elf in some other distrib.

Living in the 90's, that is :P No modern compilers I am aware of target the a.out format as their default output format for object code. Maybe it's a misleading default of GCC to put the object code into a file called a.out when no explicit output file name is specified, but if you run the file command on a.out, you'll find out that it's an ELF file. The a.out format is ancient and it's kind of "de facto obsolete".

What is the true definitions for portable executables and object code?

You've already got the Wikipedia link to object files, here's the one to "Portable Executable".

How is it that Windows and UNIX platform covers both executables annd object code under the same file format (.COFF, .elf).

Because the ELF format (and apparently COFF too) has been designed like so. And why not? It's just the very same machine code after all, it seems quite logical to use one file format during all the compilation steps. Just like we don't like when dynamic libraries and stand-alone executables have a different format. (That's why ELF is called ELF - it's an "Executable and Linkable Format".)

Am I misinterpreting "Linkable"?

I don't know. From your question it's not clear to me what you think "linkable" is. In general, it means that it's a file that can be linked against, i. e. a library.

Based on question 1. (and perhaps 2) do I need to use symbol tables (e.g. .LUM or .MAP files) with object code then? Symbols as in debug symbols and using them when re-hosting the object files on a different machine.

I think this one is not related to the executable format used. If you want to debug, you have to generate debugging information no matter what. But if you don't need to debug, then you're free to omit them of course.