Introduction
On Windows, the compiler turns each source file into a .obj object file
in the COFF format (Common Object File Format), which the linker then joins into a
PE32 or PE32+
program or library. Microsoft’s compiler in
Visual Studio writes them, and so do
Clang and other compilers that target Windows. A static library
(.lib) is an archive of these object
files.
The .obj extension has other meanings. A 3D model in the
Wavefront OBJ format is a text file with the same
extension, and older DOS and Windows compilers wrote object files in a different format,
OMF. See
all the .OBJ variations.
Structure
A COFF object file has no signature. It starts directly with the 20-byte
file header, the same header a PE
file has after its PE\0\0 signature: the target machine, the number of
sections, a timestamp, the position of the symbol table, and an optional header size
that is 0 for object files.
The section table follows, then each section’s raw data and its relocations: the places the linker must patch once it knows where everything ends up. The symbol table lists the functions and variables the file defines and the ones it needs from elsewhere, in 18-byte records, and a string table holds names longer than eight characters. COMDAT sections are ones the linker keeps only one copy of, and special sections hold linker directives and CodeView debug information.
0file header: machine, section count, timestamp, symbol table position and count, optional header size, flags20 bytes20section table: name, size and data position of each section40 bytes each…section data, usually each followed by its relocationsvaries, 10 bytes per relocation…symbol table18 bytes per record…string table: a 4-byte size, then long namesvaries
Not described
The gaps are those on the object file overview. For COFF, the relocations and symbols were not confirmed against LLVM’s tools, and CodeView record contents are shown by kind only.
Also used for
- Wavefront OBJ – A plain text mesh of vertices, normals and faces, read by almost every 3D program.
Variants
- OMF object file – Intel’s object format for 16-bit DOS and Windows compilers, in typed records
- COFF object file – compiled code from Windows compilers, waiting for the linker
- COFF object file (bigobj) – the extended form of COFF, for objects with very many sections
- Anonymous object header – the wrapper Microsoft’s compiler writes for link-time code generation
Links
Software
- Visual Studio – Microsoft’s development environment, whose
linkjoins them anddumpbinshows them (Windows) - LLVM – compiler toolkit with
lld-linkandllvm-objdump(Windows, macOS, Linux) - GNU Binutils – GNU’s binary tools, with
objdump(Windows, macOS, Linux) - Ghidra – the NSA’s free reverse engineering suite (Windows, macOS, Linux)
- IDA Pro – commercial disassembler and decompiler (Windows, macOS, Linux)
Developers
Reference
- PE Format – Microsoft’s specification of PE and COFF