parsingassemblycompilationlanguage-agnosticexecutable-format

How do compilers create the executable file at the end of the compilation process?


I've been reading on the compilation process, I understand some of the earlier concepts like parsing but I stop short of understanding how the executable file is created at the end.

In the examples I've seen around the "compiler" takes input in the form of a lang defined by BNF and then upon parsing it outputs assembly.

Is the executable file literally just that assembly in binary form? I feel like this can't be the case given that there are applications for making executables from assembly?

If this isn't answerable (ie it's too complex for the stack overflow format) I'd totally be happy with links/books so I can educate myself.


Solution

  • The compiler (or more specifically, the linker) creates the executable.

    The format of the file generally vary depending on the operating system.

    There are currently two main formats ELF and COFF

    http://en.wikipedia.org/wiki/Executable_and_Linkable_Format

    http://en.wikipedia.org/wiki/COFF

    If you understand the concept of a structure, this is the same, only within a file. Each file has a first structure called a header, and from there you can access the other structures as required.

    In most cases, only the resulting binary code is saved in these files, although you often find debug information. Some formats could save the source along the code, but now a day it only saves the necessary references to the source.

    With dynamic linking, you also find symbol tables that include the actual symbol name. Otherwise, only relocation tables would be required.

    Under the Amiga we also had the possibility to define code in a "segment". Only one segment could be loaded at a time. Once you were done with the segment, you could unload it and load another. Yet, in the end the concepts were similar. Structures in a file.

    Microsoft offers a PDF about the COFF format. I could not find it on their website just now, but it looks like others have it. ELF has many links in the Wikipedia page so you should be able to find a PDF to get started.