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

15,957

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.

Share:
15,957

Related videos on Youtube

ha9u63ar
Author by

ha9u63ar

Software Developer

Updated on June 26, 2022

Comments

  • ha9u63ar
    ha9u63ar almost 2 years

    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.

  • ha9u63ar
    ha9u63ar almost 11 years
    I am updating my question as we speak (Linkable bit). Should be better now!
  • Admin
    Admin almost 11 years
    @hagubear Sorry but I don't understand your 4th question at all. What do you mean by "use symbol tables with object code"? Use in what sense? "with the object code" in what sense?
  • ha9u63ar
    ha9u63ar almost 11 years
    Also, I "Wiki"ed it before you posted the Object code bit. Wikipedia is sometimes corrupted due to different people having different inputs on the same topic. I respect stackoverflow more than any book or website because expert users such as yourself can actually constructively criticise a problem scope and share your knowledge/understanding regarding those. Hence, my questions here instead of wikipedia :)
  • Admin
    Admin almost 11 years
    @hagubear I think now I understand it, please see edit. Well yes, Stack Overflow can be more trustworthy than Wikipedia in some cases. Also, I'm far from an expert :)
  • ha9u63ar
    ha9u63ar almost 11 years
    Thank you H2CO3 for that. Please not that I amended "compile" to "compile and assemble" as it would make sense for object files as opposed to assembler code.