Extreme C
上QQ阅读APP看书,第一时间看更新

Object file formats

As we explained in the previous chapter, Chapter 2, Compilation and Linking, on a platform, object files have their own specific format for storing machine-level instructions. Note that this is about the structure of object files and this is different from the fact that each architecture has its own instruction set. As we know from the previous discussion, these two variations are different parts of the ABI in a platform; the object file format and the architecture's instruction set.

In this section, we are going to have a brief look into some widely known object file formats. To start with, let's look at some object file formats used in various operating systems:

  • ELF used by Linux and many other Unix-like operating systems
  • Mach-O used in OS X (macOS and iOS) systems
  • PE used in Microsoft Windows

To give some history and context about the current and past object file formats, we can say that all object file formats that exist today are successors to the old a.out object file format. It was designed for early versions of Unix.

The term a.out stands for assembler output. Despite the fact that the file format is obsolete today, the name is still used as the default filename for the executable files produced by most linkers. You should remember seeing a.out in a number of examples in the first chapter of the book.

However, the a.out format was soon replaced by COFF or the Common Object File Format. COFF is the basis for ELF – the object format that we use in most Unix-like systems. Apple also replaced a.out with Mach-O as part of OS/X. Windows uses the PE or Portable Execution file format for its object files, which is based on COFF.

Note:

A deeper history of object file formats can be found here: https://en.wikipedia.org/wiki/COFF#History. Knowing about the history of a specific topic will help you to get a better understanding of its evolution path and current and past characteristics.

As you can see, all of today's major object file formats are based on the historic object file format a.out, and then COFF, and in many ways share the same ancestry.

ELF is the standard object file format used in Linux and most Unix-like operating systems. In fact, ELF is the object file format used as part of the System V ABI, heavily employed in most Unix systems. Today, it is the most widely accepted object file format used by operating systems.

ELF is the standard binary file format for operating systems including, but not limited to:

  • Linux
  • FreeBSD
  • NetBSD
  • Solaris

This means that as long as the architecture beneath them remains the same, an ELF object file created for one of these operating systems can be run and used in others. ELF, like all other file formats, has a structure that we will describe briefly in the upcoming sections.

Note:

More information about ELF and its details can be found here: https://www.uclibc.org/docs/psABI-x86_64.pdf. Note that this link refers to the System V ABI for AMD 64-bits (amd64) architecture.

You can also read the HTML version of the System V ABI here: http://www.sco.com/developers/gabi/2003-12-17/ch4.intro.html.

In the upcoming sections, we are going to talk about the temporary and final products of a C project. We start with relocatable object files.